/// <summary> /// Create unmanaged image from the specified managed image. /// </summary> /// <param name="imageData">Source locked image data.</param> /// <returns>Returns new unmanaged image, which is a copy of source managed image.</returns> /// <remarks><para>The method creates an exact copy of specified managed image, but allocated /// in unmanaged memory. This means that managed image may be unlocked right after call to this /// method.</para></remarks> public static Image FromManagedImage(BitmapData imageData) { PixelFormat pixelFormat = PixelFormatHelper.FromSystemPixelFormat(imageData.PixelFormat); // allocate memory for the image return(new Image(UnmanagedBuffer.CreateCopyFrom(imageData.Scan0, imageData.Stride * imageData.Height), imageData.Width, imageData.Height, imageData.Stride, pixelFormat)); }
/// <summary> /// Initializes a new instance of the <see cref="ImageBase"/> class. /// </summary> /// <param name="bitmapData">Locked bitmap data.</param> /// <param name="makeCopy">Indicates whether a copy is made (default is false).</param> /// <remarks> /// <para>When the <paramref name="makeCopy"/> parameter is false (default), the image simply wraps /// the bitmap data. As such, the bitmap data must stay locked for the duration of using the <see cref="ImageBase"/> object. /// </para> /// <para>If the <paramref name="makeCopy"/> parameter is set to true, a copy of the bitmap /// data is made, and the bitmap data can be released right after the <see cref="ImageBase"/> has been constructed. /// </para> /// </remarks> public ImageBase(BitmapData bitmapData, bool makeCopy = false) { this.image = makeCopy ? UnmanagedBuffer.CreateCopyFrom(bitmapData.Scan0, bitmapData.Stride * bitmapData.Height) : UnmanagedBuffer.WrapIntPtr(bitmapData.Scan0, bitmapData.Height * bitmapData.Stride); this.width = bitmapData.Width; this.height = bitmapData.Height; this.stride = bitmapData.Stride; this.pixelFormat = PixelFormatHelper.FromSystemPixelFormat(bitmapData.PixelFormat); }