سؤال

The Layout:

I'm using third party controls to scan documents. I have an event that occurs when I scan an image (mostly used to add annotations the image). The event provides me with only one property named .PAhDC. This property is a handle to the DC that stores the image before it is written to a file. Thus I can make changes to the image before it gets written to a file.

The Expected Results:

I would like to simply add a 1/4 inch (lets say 100 pixels) of white space line on the very top of the image. If my original image is 200x200 (WxH) then my new final image would need to be 200x300.

Question And Other Thoughts:

How can I alter an existing image with only knowing it's DC handle? I was thinking of doing something like the following...

  1. Create a new DC.
  2. Create a new Bitmap 100 pixels taller than the original image.
  3. Use that new bitmap in the new DC.
  4. Copy the original image to the new bitmap (100 pixels from the top as a start point).
  5. Then use something like SelectObject to replace the old bitmap in the original hDC with the new one and then destroy the old bitmap object.

Note: I would like to do this with MANAGED CODE as much as possible. Using SelectObject() was the only way I could think of but it's of course unmanaged code... :/

هل كانت مفيدة؟

المحلول

You can't without cooperation with the owner of the bitmap and DC.

The DeviceContext is purely a viewport onto an underlying DIB/bitmap and has no concept of size or dimensions (beyond the clipping region) While you can create a new bitmap and select it into the DC, it's highly likely that the application will just ignore what you've done and use the DIB that it has created. The end result of this will be GDI object leaks and no change to the underlying image.

To do what you you ask, you will need full cooperation with the other code and them adding a method that allows you to replace the underlying data.

نصائح أخرى

Sure, you can do this in managed code. All P/Invoke declarations are readily available from any decent search engine.

  • When creating a new DC, make sure it's a DC compatible with the original one
  • When creating a new bitmap, make sure it's compatible with the DC
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top