有没有办法在UserControl.OnPaint()方法中使用从JpegBitmapDecoder返回的BitmapFrame?我被告知Systems.Windows.Media.Imaging的JPEG解码性能比Systems.Windows.Forms库使用的GDI+要好得多。但是,我的应用程序已经是用Systems.Windows.Forms库编写的,我不想改变所有的东西。我所需要的是一种更快的方法来解压缩JPEG帧并将其绘制在OnPaint()方法中。
发布于 2009-12-12 07:36:05
我自己想出了答案。以下是示例代码:
JpegBitmapDecoder decoder = new JpegBitmapDecoder(pixelStream, BitmapCreateOptions.None, BitmapCacheOption.None);
BitmapFrame frame = decoder.Frames[0];
frame.CopyPixels(pixelBuffer, stride, 0);pixelBuffer是一个预分配的字节数组。然后我可以使用它来构造OnPaint()中使用的位图。
发布于 2013-05-19 16:14:41
要在Windows.Forms项目中使用它,请添加以下引用:
然后调用此方法:
protected static Bitmap JpegToBitmap(Stream jpg)
{
JpegBitmapDecoder ldDecoder = new JpegBitmapDecoder(jpg, BitmapCreateOptions.None, BitmapCacheOption.None);
BitmapFrame lfFrame = ldDecoder.Frames[0];
Bitmap lbmpBitmap = new Bitmap(lfFrame.PixelWidth, lfFrame.PixelHeight);
Rectangle lrRect = new Rectangle(0, 0, lbmpBitmap.Width, lbmpBitmap.Height);
BitmapData lbdData = lbmpBitmap.LockBits(lrRect, ImageLockMode.WriteOnly, (lfFrame.Format.BitsPerPixel == 24 ? PixelFormat.Format24bppRgb : PixelFormat.Format32bppArgb));
lfFrame.CopyPixels(System.Windows.Int32Rect.Empty, lbdData.Scan0, lbdData.Height * lbdData.Stride, lbdData.Stride);
lbmpBitmap.UnlockBits(lbdData);
return lbmpBitmap;
}https://stackoverflow.com/questions/1768644
复制相似问题