private bool CheckConvertion(MainForm form, IWICFormatConverterInfo info, Guid from, Guid to) { if (from == to) { return(true); } IWICImagingFactory factory = (IWICImagingFactory) new WICImagingFactory(); IWICPalette palette = factory.CreatePalette(); palette.InitializePredefined(WICBitmapPaletteType.WICBitmapPaletteTypeFixedBW, false); IWICBitmap bitmap = null; IWICFormatConverter converter = null; try { try { converter = info.CreateInstance(); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e)); return(false); } try { bitmap = factory.CreateBitmap(1, 1, from, WICBitmapCreateCacheOption.WICBitmapCacheOnLoad); bitmap.SetPalette(palette); } catch (Exception e) { form.Add(this, e.TargetSite.ToString(Resources._0_Failed), new DataEntry(e), new DataEntry(Resources.PixelFormat, from)); return(false); } try { converter.Initialize(bitmap, to, WICBitmapDitherType.WICBitmapDitherTypeNone, palette, 0, WICBitmapPaletteType.WICBitmapPaletteTypeCustom); } catch (Exception e) { form.CheckHRESULT(this, WinCodecError.WINCODEC_ERR_UNSUPPORTEDPIXELFORMAT, e, new DataEntry(Resources.Source, from), new DataEntry(Resources.Destination, to)); return(false); } return(true); } finally { palette.ReleaseComObject(); converter.ReleaseComObject(); bitmap.ReleaseComObject(); factory.ReleaseComObject(); } }
public static IWICBitmap ToWic(IWICComponentFactory factory, Bitmap bit) { Guid pixelFormat = ConversionUtils.FromPixelFormat(bit.PixelFormat); if (pixelFormat == Guid.Empty) { throw new NotSupportedException("PixelFormat " + bit.PixelFormat.ToString() + " not supported."); } BitmapData bd = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadOnly, bit.PixelFormat); IWICBitmap b = null; IWICPalette p = null; try { //Create WIC bitmap directly from unmanaged memory long result = CreateBitmapFromMemory(factory, (uint)bit.Width, (uint)bit.Height, ref pixelFormat, (uint)bd.Stride, (uint)(bd.Stride * bd.Height), bd.Scan0, out b); //b = factory.CreateBitmapFromMemory((uint)bit.Width, (uint)bit.Height, ConversionUtils.FromPixelFormat(bit.PixelFormat), (uint)bd.Stride, (uint)(bd.Stride * bd.Height), bd.Scan0); if (result == 0x80070057) { throw new ArgumentException(); } if (result < 0) { throw new Exception("HRESULT " + result); } //Copy the bitmap palette if it exists var sPalette = bit.Palette; if (sPalette.Entries.Length > 0) { p = factory.CreatePalette(); uint[] colors = new uint[sPalette.Entries.Length]; for (int i = 0; i < sPalette.Entries.Length; i++) { colors[i] = (uint)(((sPalette.Entries[i].A << 24) | (sPalette.Entries[i].R << 16) | (sPalette.Entries[i].G << 8) | sPalette.Entries[i].B) & 0xffffffffL); } p.InitializeCustom(colors, (uint)colors.Length); b.SetPalette(p); } return(b); } finally { bit.UnlockBits(bd); if (p != null) { Marshal.ReleaseComObject(p); } } }