我想调整大小并异步保存一批下载的图像。这样做的最佳方法是什么?
目前我在下载图片后调用此方法:
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();
}
有没有办法异步执行此操作?
答案 0 :(得分:0)
如果您要处理大量图像,生产者/消费者模式可能比为每个图像启动任务更好。将您的数据放入BlockingCollection,并启动一个长时间运行的Task,它使用bc.GetConsumingEnumerable()检索数据并处理它们。