2014-07-08 112 views
0

我需要将图像保存为PNG格式。不幸的是旧设备(采用WinCE 5.0)图像只能保存为BMP格式:检查设备是否支持以JPEG和PNG格式保存

try 
{ 
    destinationBmp.Save(fileName+".png", ImageFormat.Png); 
} 
catch (NotSupportedException) 
{ 
    // No PNG & JPG support on Windows CE 5.0 devices 
    // 
    destinationBmp.Save(fileName+".bmp", ImageFormat.Bmp); 
} 
finally 
{ 
    destinationBmp.Dispose(); 
} 

有可能没有处理NotSupportedException异常检查图像格式支持?

+1

我还没有做过任何研究,但我会打赌答案涉及使用P/Invoke访问GDI以获取支持的编解码器列表。 Guven这将需要更多的努力,我认为你使用try/catch更好。 – Dai

+1

ImageCodecInfo类在CF上不可用。使用Environment.OSVersion跳转到头脑中。 –

+0

我会反思,并检查System.Drawing.Imaging中是否存在属性ImageFormat.Bmp。以下是使用反射检查属性的问题/答案:http://stackoverflow.com/questions/15341028/check-if-a-property-exist-in-a-class – josef

回答

3

Windows CE 5.0能够支持JPEG和PNG编码,但它取决于您的设备供应商如何配置操作系统映像。如果他们没有包含编解码器,那么它将无法工作。如果您是创建Windows映像的供应商,则可以使用平台构建器将这些添加到您的设备中。

您正在使用的方法可能是决定它是否能够保存PNG或JPG图像的最简单方法。然而,从一般意义上说,如果它不支持PNG,它也不支持JPG(看你的try/catch代码,你需要每种格式的try/catch),那么它就不是正确的。

如果您有关于代码运行的所有可能设备的知识,那么使用@HansPassant的检查Environment.OSVersion的建议对您来说可能是合理的,因为您可以决定它是您知道的Win CE 5.0设备之一不支持JPG和PNG。否则,假定Windows CE 5.0不可用是不正确的。

如果您想要确定安装哪些编解码器的确切方法,您可以通过与Imaging COM InterfaceReference Here)进行交互来找到该解决方案。具体而言,您需要致电IImagingFactory.GetInstalledEncoders

我的建议可能是保持简单并创建一个测试图像,并尝试将其保存到每个格式为MemoryStream一次,并捕获NotSupportedExceptions以确定设备的功能。这样你就不需要创建一个文件。

注:

的COM进口会是这个样子,但你需要填写你以正确的参数希望GetInstallEncoders方法的占位符(对不起,我没有使用这个) 。然后调用Activator.CreateInstance(...)您需要熟悉与.NET中的COM接口进行交互。

[ComImport, Guid("327ABDA7-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] 
    [ComVisible(true)] 
    public interface IImagingFactory 
    { 
     uint CreateImageFromStream();  // This is a place holder, note the lack of arguments 
     uint CreateImageFromFile(string filename, out IImage image); 
     // We need the MarshalAs attribute here to keep COM interop from sending the buffer down as a Safe Array. 
     uint CreateImageFromBuffer([MarshalAs(UnmanagedType.LPArray)] byte[] buffer, uint size, BufferDisposalFlag disposalFlag, out IImage image); 
     uint CreateNewBitmap(uint width, uint height, PixelFormatID pixelFormat, out IBitmapImage bitmap); 
     uint CreateBitmapFromImage(IImage image, uint width, uint height, PixelFormatID pixelFormat, InterpolationHint hints, out IBitmapImage bitmap); 
     uint CreateBitmapFromBuffer();  // This is a place holder, note the lack of arguments 
     uint CreateImageDecoder();   // This is a place holder, note the lack of arguments 
     uint CreateImageEncoderToStream(); // This is a place holder, note the lack of arguments 
     uint CreateImageEncoderToFile(); // This is a place holder, note the lack of arguments 
     uint GetInstalledDecoders();  // This is a place holder, note the lack of arguments 
     uint GetInstalledEncoders();  // This is a place holder, note the lack of arguments 
     uint InstallImageCodec();   // This is a place holder, note the lack of arguments 
     uint UninstallImageCodec();   // This is a place holder, note the lack of arguments 
    } 
相关问题