如何在c中异步调整大小和保存图像

本文关键字:保存 图像 调整 异步 | 更新日期: 2023-09-27 18:21:56

我想异步调整和保存一批下载的图像。做到这一点的最佳方法是什么?

目前我在下载图像后调用此方法:

    public void SaveToResizedBitmap(Bitmap pBitmap, int pWidth, int pHeight, string pOutputFileNameStr)
    {
        System.Drawing.Image origImage = pBitmap;
        System.Drawing.Image origThumbnail = new Bitmap(pWidth, pHeight, origImage.PixelFormat);
        Graphics oGraphic = Graphics.FromImage(origThumbnail);
        oGraphic.CompositingQuality = CompositingQuality.HighQuality;
        oGraphic.SmoothingMode = SmoothingMode.HighQuality;
        oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
        Rectangle oRectangle = new Rectangle(0, 0, pWidth, pHeight);
        oGraphic.DrawImage(origImage, oRectangle);
        string lLowerFileNameStr = pOutputFileNameStr.ToLower();
        if (lLowerFileNameStr.Contains(".png"))
        {
            // Save the file in PNG format
            origThumbnail.Save(pOutputFileNameStr, ImageFormat.Png);
        }
        if (lLowerFileNameStr.Contains(".jpg"))
        {
            // Save the file in JPG format
            origThumbnail.Save(pOutputFileNameStr, ImageFormat.Jpeg);
        }
        origImage.Dispose();
    }

有没有异步的方法?

如何在c中异步调整大小和保存图像

如果有很多图像要处理,那么生产者/消费者模式可能比为每个图像启动Task要好。将您的数据放入BlockingCollection中,然后启动一个长时间运行的Task,该Task使用bc检索数据。GetConsumingEnumerable()并对其进行处理。