Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating Bitmap from part of an image with BitmapImage.Create
    text
    copied!<p>Background:</p> <p>I'm working with an external (unmanaged) API which will render an image for me and pass the memory location of the image(as well as stride, etc.) back. This API is not yet fully developed, so I'm trying to mock the behaviour for the time being.</p> <p>Task:</p> <p>I'm trying to create an image from part of the image which is created in the unmanaged code. For example, I would like to create a BitmapSource which contains only the right half of the image.</p> <p>To simluate this, I create an image in memory:</p> <pre><code> Bitmap logo = Properties.Resources.test2; BitmapData bmpData = logo.LockBits(new System.Drawing.Rectangle(0, 0, 970, 844), System.Drawing.Imaging.ImageLockMode.ReadWrite, logo.PixelFormat); </code></pre> <p>Now I can get the starting point of the image in memory with</p> <pre><code>IntPtr startOfImage = bmpData.Scan0; </code></pre> <p>To get the right hand side of the image, i'm unsuccessfully trying (assuming the image dimensions are 970*844 and the Pixelformat is 24bit RGB):</p> <pre><code> IntPtr locationOfImage = bmpData.Scan0 += bmpData.Stride / 2 ; BitmapSource source = BitmapImage.Create(485, 844, 96, 96, System.Windows.Media.PixelFormats.Rgb24, null, locationOfImage, 844 * bmpData.Stride / 2, bmpData.Stride / 2); </code></pre> <p>However, this is no way is giving me the right hand side of the image, what am I missing?</p> <p>Best Whishes</p> <p>Daniel</p> <p>EDIT:</p> <p>It seems clear that this is not returning the right hand side of the image because only for the first line I am skipping the half of the stride. Is it somehow possible to create an image while always skipping part of the bytes (e.g. have a skip value for on each line skipping a part of the line)?</p> <p>Obviously, I also could first read in all necessary bytes into an array and pass it instead of the IntPtr, however it would be nice to be able to directly pass the location.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload