[英]I'm trying to get a list of images from website and also save them to hard disk but it doesn't work
[英]How can i get images from the hard disk resize the images and add them to a list<image> fast?
我现在在做
imageslist = new List<Image>();
foreach (string myFile in
Directory.GetFiles(dir, "*.bmp", SearchOption.AllDirectories))
{
Bitmap bmp = new Bitmap(myFile);
imageslist.Add(bmp);
}
但是foreach
非常慢。 而且我有这种方法来调整图像的大小,我想在将它们添加到列表之前调整它们的大小
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
最后,我想在List<Image> imageslist
所有分辨率为100,100的图像,并且这些图像现在是24位深度,我是否也应该尝试更改它,否则24位就可以了?
我有两个改进:
var imageslist = Directory.EnumerateFiles(dir, "*.bmp", SearchOption.AllDirectories)
.AsParallel()
.Select(path => new Bitmap(path))
.Select(bmp => ResizeImage(bmp, 100, 100))
.ToList();
请记住要验证并行解决方案的速度,因为只有在将其与非并行解决方案进行比较(在不使用AsParallel的情况下运行代码)之后,您才能确保它可以提高性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.