private void CopyColormap(Bitmap img, Pix pix) { var imgPalette = img.Palette; var imgPaletteEntries = imgPalette.Entries; System.Drawing.Color ss = new System.Drawing.Color(); var pixColormap = PixColormap.Create(pix.Depth); try { for (int i = 0; i < imgPaletteEntries.Length; i++) { System.Drawing.Color color = System.Drawing.Color.FromArgb(imgPaletteEntries[i].A, imgPaletteEntries[i].R, imgPaletteEntries[i].G, imgPaletteEntries[i].B); if (!pixColormap.AddColor((PixColor)color)) { throw new InvalidOperationException(String.Format("Failed to add colormap entry {0}.", i)); } } pix.Colormap = pixColormap; } catch (Exception) { pixColormap.Dispose(); throw; } }
/// <summary> /// Creates a new pix instance using an existing handle to a pix structure. /// </summary> /// <remarks> /// Note that the resulting instance takes ownership of the data structure. /// </remarks> /// <param name="handle"></param> private Pix(IntPtr handle) { if (handle == IntPtr.Zero) { throw new ArgumentNullException("handle"); } this.handle = new HandleRef(this, handle); this.width = Interop.LeptonicaApi.Native.pixGetWidth(this.handle); this.height = Interop.LeptonicaApi.Native.pixGetHeight(this.handle); this.depth = Interop.LeptonicaApi.Native.pixGetDepth(this.handle); var colorMapHandle = Interop.LeptonicaApi.Native.pixGetColormap(this.handle); if (colorMapHandle != IntPtr.Zero) { this.colormap = new PixColormap(colorMapHandle); } }