本文介绍了ImageFormat.Png的NotSupportedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将一个屏幕截图写入MemoryStream时遇到了一些困难。  下面是代码:


 private static byte [] GetScreenCapture()
{
IntPtr hdc = GetDC(IntPtr.Zero);
尺寸大小= ScreenDimensions;
位图位图=新位图(size.Width,size.Height,PixelFormat.Format16bppRgb565);
byte [] buffer = null;

使用(Graphics g = Graphics.FromImage(bitmap))
{
IntPtr destHdc = g.GetHdc();
BitBlt(destHdc,0,0,size.Width,size.Height,hdc,0,0,RasterOperation.SRC_COPY);
g.ReleaseHdc(destHdc);

MemoryStream memStream = new MemoryStream();
bitmap.Save(memStream,ImageFormat.Png); //< - 这里发生错误! ImageFormat.Bmp可以工作,但所有其他格式都没有!

buffer = memStream.ToArray();
memStream.Close();
}

返回缓冲区;
}

这适用于我正在使用的WinCE 6.0设备,但WinCE上的代码相同4.2设备导致NotSupportedException。  我猜测早期版本的WinCE不支持所有不同的ImageFormat选项。  如注释
代码中所述,ImageFormat.Bmp可以工作,但我需要使用ImageFormat.Png。  我还有其他选择吗?



谢谢,

Zac

解决方案

I'm having some difficulty writing out a png of a screenshot to a MemoryStream.  Below is the code:

private static byte[] GetScreenCapture()
{
    IntPtr hdc = GetDC(IntPtr.Zero);
    Size size = ScreenDimensions;
    Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format16bppRgb565);
    byte[] buffer = null;
    
    using (Graphics g = Graphics.FromImage(bitmap))
    {
        IntPtr destHdc = g.GetHdc();
        BitBlt(destHdc, 0, 0, size.Width, size.Height, hdc, 0, 0, RasterOperation.SRC_COPY);
        g.ReleaseHdc(destHdc);

        MemoryStream memStream = new MemoryStream();
        bitmap.Save(memStream, ImageFormat.Png); // <-- Error occurs here!  ImageFormat.Bmp works but all other formats do not!

        buffer = memStream.ToArray();
        memStream.Close();
    }

    return buffer;
}

This works great on a WinCE 6.0 device that I'm using but the same code on an WinCE 4.2 device results in a NotSupportedException.  I'm guessing earlier versions of WinCE don't support all the different ImageFormat options.  As noted in the commented code, ImageFormat.Bmp works but I need to use ImageFormat.Png.  What other options do I have?

Thanks,
Zac

解决方案


这篇关于ImageFormat.Png的NotSupportedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 05:41