0

Background:

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.

Task:

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.

To simluate this, I create an image in memory:

 Bitmap logo = Properties.Resources.test2;
 BitmapData bmpData =
            logo.LockBits(new System.Drawing.Rectangle(0, 0, 970, 844), System.Drawing.Imaging.ImageLockMode.ReadWrite, logo.PixelFormat);

Now I can get the starting point of the image in memory with

IntPtr startOfImage = bmpData.Scan0;

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):

 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);

However, this is no way is giving me the right hand side of the image, what am I missing?

Best Whishes

Daniel

EDIT:

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)?

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.

Daniel
  • 471
  • 5
  • 28
  • check this link: http://stackoverflow.com/questions/734930/how-to-crop-an-image-using-c – Boomer Dec 27 '12 at 12:22
  • Thanks for the input, however cropping an image would result in the copy of the image data for every piece of the image. Right now I'm trying to clip the image for every part I want to display. (With the similar Clip function, maybe this works...). The idea is to clip the image, render an image part, then clip it again etc. – Daniel Dec 27 '12 at 12:29

0 Answers0