/// <summary> /// Function to create a WIC bitmap from a System.Drawing.Image object. /// </summary> /// <param name="bitmap">WIC bitmap to copy to our image data.</param> /// <param name="filter">Filter used to scale the image.</param> /// <param name="ditherFlags">Flags used to dither the image.</param> /// <param name="buffer">Buffer for holding the image data.</param> /// <param name="clip">TRUE to clip the data, FALSE to scale it.</param> public void AddWICBitmapToImageData(WIC.Bitmap bitmap, ImageFilter filter, ImageDithering ditherFlags, GorgonImageBuffer buffer, bool clip) { Guid conversionFormat = GetGUID(buffer.Format); bool needsResize = (buffer.Width != bitmap.Size.Width) || (buffer.Height != bitmap.Size.Height); if (conversionFormat == Guid.Empty) { throw new GorgonException(GorgonResult.FormatNotSupported, string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, buffer.Format)); } // Turn off filtering if we're not resizing. if (!needsResize) { filter = ImageFilter.Point; } // If the pixel format of the bitmap is not the same as our // conversion format, then we need to convert the image. if (bitmap.PixelFormat != conversionFormat) { ConvertFormat(bitmap.PixelFormat, conversionFormat, (WIC.BitmapDitherType)ditherFlags, (WIC.BitmapInterpolationMode)filter, bitmap, null, 0, buffer, needsResize, clip); } else { // Just dump without converting because our formats are equal. if ((!needsResize) || (!ResizeBitmap(bitmap, (WIC.BitmapInterpolationMode)filter, buffer, clip))) { bitmap.CopyPixels(buffer.PitchInformation.RowPitch, buffer.Data.BasePointer, buffer.PitchInformation.SlicePitch); } } }
/// <summary> /// Function to create a WIC bitmap from a System.Drawing.Image object. /// </summary> /// <param name="image">Image to convert.</param> public WIC.Bitmap CreateWICImageFromImage(Image image) { BitmapData bmpData = null; var imageBitmap = image as Bitmap; bool bitmapClone = false; if (image == null) { throw new ArgumentNullException("image"); } // If the image being passed is not a bitmap, then make it into one. // Or, if the image is a 32bpp RGB (without alpha), or if the image is indexed. // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags if ((imageBitmap == null) || (image.PixelFormat == PixelFormat.Format32bppRgb) || ((image.PixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed)) { imageBitmap = new Bitmap(image); bitmapClone = true; } try { // Try to get a compatible WIC format. Guid guid = GetGUID(imageBitmap.PixelFormat); if (guid == Guid.Empty) { throw new GorgonException(GorgonResult.FormatNotSupported, string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, image.PixelFormat)); } bmpData = imageBitmap.LockBits(new Rectangle(0, 0, imageBitmap.Width, imageBitmap.Height), ImageLockMode.ReadOnly, imageBitmap.PixelFormat); var pointer = new DX.DataRectangle(bmpData.Scan0, bmpData.Stride); var result = new WIC.Bitmap(Factory, imageBitmap.Width, imageBitmap.Height, guid, pointer, bmpData.Stride * bmpData.Height); result.SetResolution(image.HorizontalResolution, image.VerticalResolution); return(result); } finally { if (bmpData != null) { imageBitmap.UnlockBits(bmpData); } if (bitmapClone) { imageBitmap.Dispose(); } } }
/// <summary> /// Function to convert a WIC bitmap to a System.Drawing.Image /// </summary> /// <param name="bitmap">Bitmap to convert.</param> /// <param name="format">Pixel format to use.</param> /// <returns>The converted bitmap.</returns> public Image CreateGDIImageFromWICBitmap(WIC.Bitmap bitmap, PixelFormat format) { Bitmap result = null; BitmapData lockData = null; Guid conversionFormat = GetGUID(format); if (conversionFormat == Guid.Empty) { throw new GorgonException(GorgonResult.FormatNotSupported, string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, format)); } try { // Create the new bitmap. result = new Bitmap(bitmap.Size.Width, bitmap.Size.Height, format); lockData = result.LockBits(new Rectangle(0, 0, bitmap.Size.Width, bitmap.Size.Height), ImageLockMode.WriteOnly, format); // We need to convert, so copy using the format converter. if (bitmap.PixelFormat != conversionFormat) { using (var converter = new WIC.FormatConverter(Factory)) { converter.Initialize(bitmap, conversionFormat, WIC.BitmapDitherType.None, null, 0, WIC.BitmapPaletteType.Custom); converter.CopyPixels(lockData.Stride, lockData.Scan0, lockData.Stride * lockData.Height); } } else { // Otherwise, copy it all in one shot. bitmap.CopyPixels(lockData.Stride, lockData.Scan0, lockData.Stride * lockData.Height); } return(result); } finally { if (lockData != null) { result.UnlockBits(lockData); } } }
/// <summary> /// Function to create a list of WIC bitmaps from Gorgon image data. /// </summary> /// <param name="data">Data to convert to the list of WIC bitmaps.</param> /// <returns>The list of WIC bitmaps.</returns> public WIC.Bitmap[] CreateWICBitmapsFromImageData(GorgonImageData data) { int bitmapIndex = 0; Guid bitmapFormat = GetGUID(data.Settings.Format); if (bitmapFormat == Guid.Empty) { throw new GorgonException(GorgonResult.FormatNotSupported, string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, data.Settings.Format)); } // Make room for all the buffers. var bitmaps = new WIC.Bitmap[data.Buffers.Count]; // Copy to the bitmap. foreach (var buffer in data.Buffers) { var pointer = new DX.DataRectangle(buffer.Data.BasePointer, buffer.PitchInformation.RowPitch); bitmaps[bitmapIndex] = new WIC.Bitmap(Factory, buffer.Width, buffer.Height, bitmapFormat, pointer, pointer.Pitch * buffer.Height); bitmapIndex++; } return(bitmaps); }