C# 如何在c中异步调整和保存图像#

C# 如何在c中异步调整和保存图像#,c#,asynchronous,C#,Asynchronous,我想异步调整和保存一批下载的图像。最好的方法是什么 目前我在下载图像后调用此方法: public void SaveToResizedBitmap(Bitmap pBitmap, int pWidth, int pHeight, string pOutputFileNameStr) { System.Drawing.Image origImage = pBitmap; System.Drawing.Image origThumbnail = new

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

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

    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中,然后启动一个长时间运行的任务,该任务使用bc.GetConsumingEnumerable()检索数据并对其进行处理。

这里没有真正的异步操作,您可以使用
Task.Run
将其委托给线程池。顺便说一句,您应该处理
oGraphic
实例。如果您只想让UI在保存过程中负责,请使用Task.Run,正如@SriramSakthivel所说的那样。@SriramSakthivel谢谢您的评论,我将尝试Task.Run。我希望有异步解决方案可以调整大小。感谢您的回答,我将对此进行研究(以前从未使用过)。