繁体   English   中英

如何异步调整大小并在C#中保存图像

[英]how to async resize and save image in c#

我想调整大小并异步保存一批下载的图像。 最好的方法是什么?

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

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

有没有一种异步的方法?

如果要处理大量图像,则生产者/消费者模式可能比为每个图像启动任务更好。 将您的数据放入BlockingCollection中,然后启动一个长期运行的Task,该任务使用bc.GetConsumingEnumerable()检索数据并进行处理。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM