在.net框架中获取图片宽高有多种方法,本文将详细介绍四种方法,并对其性能进行评估。如果您需要处理大量图片,这些信息将非常有用。
本文将评估以下四种方法来获取图片的宽高:
System.Drawing.Imaging.Metafile
Metafile是图片的元数据,它实际上是微软Windows系统的一种图片格式,如WMF和EMF(Windows Metafile和Enhanced Metafile)。虽然它可以读取其他格式的图片,但不适合通过读取元数据头来提升性能。var header = Metafile.FromFile(@"D:\blog.walterlv.com\large-background-image.jpg"); var witdh = header.Width; var height = header.Height;
System.Drawing.Bitmap
var bitmap = new Bitmap(@"D:\blog.walterlv.com\large-background-image.jpg"); var witdh = bitmap.Width; var height = bitmap.Height;
System.Windows.Media.Imaging.BitmapImage
var bitmap = new BitmapImage(new Uri(@"D:\blog.walterlv.com\large-background-image.jpg", UriKind.Absolute)); var witdh = bitmap.Width; var height = bitmap.Height;
System.Windows.Media.Imaging.BitmapDecoder
BitmapImage要好得多。var decoder = new JpegBitmapDecoder(new Uri(@"D:\blog.walterlv.com\large-background-image.jpg", UriKind.Absolute), BitmapCreateOptions.DelayCreation, BitmapCacheOption.OnDemand); var frame = decoder.Frames[0]; var witdh = frame.PixelWidth; var height = frame.PixelHeight;
为了测试性能,我使用了一张非常大的图片,并对其进行了多次运行。

分别运行以上四个方法各10次:

分别运行以上四个方法各100次(可以发现大量的GC):

现在,使用不同的图片运行多次。
分别运行以上四个方法各10张图片:

分别运行以上四个方法各100张图片(可以发现大量的GC):

对于同一张图片运行不同次数:
| 消耗时间(ms) | Metafile | Bitmap | BitmapImage | BitmapDecoder |
|---|---|---|---|---|
| 1次 | 175 | 107 | 71 | 2 |
| 10次 | 1041 | 1046 | 63 | 17 |
| 100次 | 10335 | 10360 | 56 | 122 |

对于不同图片运行不同次数:
| 消耗时间(ms) | Metafile | Bitmap | BitmapImage | BitmapDecoder |
|---|---|---|---|---|
| 1次 | 175 | 107 | 71 | 2 |
| 10次 | 998 | 980 | 83 | 20 |
| 100次 | 10582 | 10617 | 255 | 204 |
| 1000次 | 127023 | 128627 | 3456 | 4015 |

可以发现,对于.NET框架中原生自带的获取图片尺寸的方法来说:
System.Windows.Media.Imaging.BitmapDecoder 的整体性能是最好的。System.Windows.Media.Imaging.BitmapImage 的运行时间不随次数的增加而增加,其内部有缓存。以上就是.NET 程序如何获取图片的宽高(框架自带多种方法的不同性能)的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号