简体   繁体   English

如何从硬盘获取图像,调整图像大小并将其添加到列表中 <image> 快速?

[英]How can i get images from the hard disk resize the images and add them to a list<image> fast?

I'm doing now 我现在在做

imageslist = new List<Image>();
            foreach (string myFile in
                      Directory.GetFiles(dir, "*.bmp", SearchOption.AllDirectories))
            {

                Bitmap bmp = new Bitmap(myFile);
                imageslist.Add(bmp);
            }

But the foreach is very slow. 但是foreach非常慢。 And i have this method to resize the images i want to resize them before adding them to the list 而且我有这种方法来调整图像的大小,我想在将它们添加到列表之前调整它们的大小

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;
        }

In the end i want to have in the List<Image> imageslist all the images in size of 100,100 resolution and the images are now 24 bit depth should i try to change it also or 24 bit is fine ? 最后,我想在List<Image> imageslist所有分辨率为100,100的图像,并且这些图像现在是24位深度,我是否也应该尝试更改它,否则24位就可以了?

I have two improvements: 我有两个改进:

  1. Use Directory.EnumerateFiles instead of Directory.GetFiles , you will not have to wait until all results will be returned, it will be lazy evaluated 使用Directory.EnumerateFiles而不是Directory.GetFiles ,您不必等到所有结果都将被返回,它将被延迟评估
  2. Run the resizing in parallel (in my example below using AsParallel extension method) 并行运行调整大小(在下面的示例中,使用AsParallel扩展方法)

var imageslist = Directory.EnumerateFiles(dir, "*.bmp", SearchOption.AllDirectories)
    .AsParallel()
    .Select(path => new Bitmap(path))
    .Select(bmp => ResizeImage(bmp, 100, 100))
    .ToList();

Remember to verify the speed of the parallel solution because only after comparing it with non-parallel solution (run the code without AsParallel ) you will be sure that it improves performance in your case. 请记住要验证并行解决方案的速度,因为只有在将其与非并行解决方案进行比较(在不使用AsParallel的情况下运行代码)之后,您才能确保它可以提高性能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试从网站获取图像列表,并将它们保存到硬盘但它不起作用 - 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 load images from the hard disk and display them in pictureBox with timer? 如何从网站下载图像到我的硬盘? - How can I download the images from the site to my hard disk? 如何将一些bat文件添加到我的项目中,这样我就不再需要硬盘上的文件了? - How can i add some bat files to my project so i will not need them anymore on the hard disk? 如何整洁地将图像添加到剪贴板并粘贴? - How can I cleanly programatically add images to the clipboard and paste them? 如何将硬盘png上的图像转换为透明图像? - How can I convert image on the hard disk png to be transparent? 如何通过选择/更改图像kb来调整图像大小? 而不是分辨率 - How can i resize images by selecting/changing the images kb ? And not resolution 如何将C#中的图像大小调整为某个硬盘大小? - How to resize an image in C# to a certain hard-disk size? 如何使用“Directory.getFiles”获取具有特定扩展名的磁盘上的所有文件并将其保存在列表中 - How can i get all files on disk with a specific extension using 'Directory.getFiles' and save them in a list 如何在上传时调整图像大小以减小图像大小? - How do i resize images on Upload to reduce the image size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM