/// <summary> /// Fast copy of managed pixel array data into System.Drawing.Bitmap image. /// No checking of passed parameters, therefore, it is a caller responsibility /// to provid valid parameter values. /// </summary> /// <param name="width"> /// Image width <see cref="System.Int32"/> in pixels /// </param> /// <param name="height"> /// Image height <see cref="System.Int32"/> in pixels /// </param> /// <param name="data"> /// Pointer <see cref="System.IntPtr"/> to buffer with image data /// </param> /// <param name="length"> /// Length <see cref="System.Int64"/> of buffer in bytes /// </param> /// <param name="format"> /// Format of image pixel expressed with <see cref="System.Drawing.Imaging.PixelFormat"/> enumeration /// </param> /// <returns> /// <see cref="System.Drawing.Bitmap"/> created with data copied from Data buffer /// of this instance of <see cref="DjvuNet.Graphics.Map"/> /// </returns> public static System.Drawing.Bitmap CopyDataToBitmap( int width, int height, IntPtr data, long length, PixelFormat format) { System.Drawing.Bitmap bmp = null; BitmapData bmpData = null; try { bmp = new System.Drawing.Bitmap(width, height, format); bmpData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); MemoryUtilities.MoveMemory(bmpData.Scan0, data, length); } catch (Exception ex) { throw new DjvuAggregateException(ex); } finally { bmp?.UnlockBits(bmpData); } return(bmp); }
/// <summary> /// Converts the pixel data to a bitmap image /// </summary> /// <param name="pixels"></param> /// <returns></returns> internal static unsafe System.Drawing.Bitmap ConvertDataToImage(int[] pixels, int width, int height) { if (width <= 0 || height <= 0) { return(null); } System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, PixelFormat.Format24bppRgb); BitmapData bits = bmp.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bmp.PixelFormat); // Value of 4 is the size of PixelFormat.Format32bppArgb // keep it synchronized with used Bitmap PixelFormat int bytesPerRow = width * 4; fixed(int *pixelsPtr = pixels) { byte *pixelsNativePtr = (byte *)pixelsPtr; for (int y = 0; y < height; y++) { var rowPtr = (int *)((byte *)bits.Scan0 + (y * bits.Stride)); pixelsNativePtr += (y * bytesPerRow); MemoryUtilities.MoveMemory(rowPtr, pixelsNativePtr, bytesPerRow); } } bmp.UnlockBits(bits); return(bmp); }
/// <summary> /// Utility conversion method allowing to convert object implementing <see cref="DjvuNet.Graphics.IMap"/> /// interface to <see cref="System.Drawing.Bitmap"/> object. /// </summary> /// <param name="map"></param> /// <param name="rect"></param> /// <param name="format"></param> /// <returns>Returns <see cref="System.Drawing.Bitmap"/> object which should be disposed after use by caller. </returns> public static System.Drawing.Bitmap ImageFromMap(GMap map, Rectangle rect, PixelFormat format) { Bitmap retVal = new Bitmap(rect.Width, rect.Height, format); BitmapData bmpData = retVal.LockBits(rect, ImageLockMode.WriteOnly, format); int pixelSize = GetPixelSize(format); int bytesPerRow = pixelSize * rect.Width; GCHandle hMapData = GCHandle.Alloc(map.Data, GCHandleType.Pinned); IntPtr pMapData = hMapData.AddrOfPinnedObject(); for (int i = 0; i < rect.Height; i++) { IntPtr destPtr = bmpData.Scan0 + (bmpData.Stride * i); IntPtr srcPtr = (IntPtr)((long)pMapData + (i * bytesPerRow)); MemoryUtilities.MoveMemory(destPtr, srcPtr, bytesPerRow); } if (hMapData.IsAllocated) { hMapData.Free(); } retVal.UnlockBits(bmpData); return(retVal); }
public static Bitmap CopyDataToBitmap(int width, int height, IntPtr data, long length, PixelFormat format, int bytesPerSrcRow = 0) { Bitmap bmp = null; BitmapData bmpData = null; try { bmp = new Bitmap(width, height, format); bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); int pixelSize = Image.GetPixelFormatSize(bmp.PixelFormat) / 8; int bytesPerRow = bytesPerSrcRow == 0 ? bmp.Width * pixelSize : bytesPerSrcRow; IntPtr dataPtr = bmpData.Scan0; for (int i = 0; i < height; i++) { MemoryUtilities.MoveMemory(dataPtr, data, bytesPerRow); dataPtr = (IntPtr)((long)dataPtr + bmpData.Stride); data = (IntPtr)((long)data + bytesPerRow); } } catch (Exception ex) { throw new DjvuAggregateException(ex); } finally { bmp?.UnlockBits(bmpData); } return(bmp); }
public static IPixelMap PixelMapFromBitmap(System.Drawing.Bitmap bmp) { IPixelMap pixMap = PixelMapTests.CreateInitVerifyPixelMap(bmp.Width, bmp.Height, Pixel.WhitePixel); BitmapData data = null; try { data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); unsafe { // TODO: Fix me! fixed(sbyte *dest = pixMap.Data) MemoryUtilities.MoveMemory(dest, (void *)data.Scan0, pixMap.Data.Length); } } catch (Exception ex) { throw new DjvuAggregateException( $"Error with bitmap. Width: {bmp.Width}, Height: {bmp.Height}, PixelFormat: {bmp.PixelFormat}", ex); } finally { if (data != null) { bmp.UnlockBits(data); } } return(pixMap); }
public static Bitmap ImageFromMap(GMap map, Rectangle rect, PixelFormat format) { Bitmap retVal = new Bitmap(rect.Width, rect.Height, format); BitmapData bmpData = retVal.LockBits(rect, ImageLockMode.WriteOnly, format); GCHandle hMapData = GCHandle.Alloc(map.Data, GCHandleType.Pinned); IntPtr pMapData = hMapData.AddrOfPinnedObject(); MemoryUtilities.MoveMemory(bmpData.Scan0, pMapData, map.Data.Length); hMapData.Free(); retVal.UnlockBits(bmpData); return(retVal); }