ImageFormat.Png的NotSupportedException [英] NotSupportedException for ImageFormat.Png

查看:191
本文介绍了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

解决方案

Windows CE是一个模块化操作系统,这意味着几乎所有东西都是一个选项。这包括对特定图像编码器/解码器的支持。看起来您的某个设备在操作系统中有此选项而另一个没有。


如果您需要PNG(为什么?),那么您有多种选择:


1。查看设备制造商是否提供支持PNG的操作系统映像。 


2。将设备替换为具有PNG支持选项的设备。


3。实施您自己的代码以PNG格式保存图像(可行但不容易)或使用第三方代码(如果可用)。


4。以支持的任何格式保存图像(例如BMP)并在服务器上转换为PNG或实际使用图像(图像大小可能会增加)。



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

解决方案

Windows CE is a modular OS which means pretty much everything is an option. That includes support for specific image encoder/decoder. It seems like one of your devices has this option in the OS and another one does not.

If you need PNGs (why?) then you have number of options:

1. See if device manufacturer offers an OS image which has PNG support. 

2. Replace devices you have with devices which have PNG support option.

3. Implement your own code to save images in PNG format (doable but not easy) or use 3rd party code if available.

4. Save image in whatever format is supported (e.g. BMP) and convert to PNG on a server or where image is actually used (image size may be increased).


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆