Drawing directly to a Window like you would using Bitmap.LockBits()and Scan0

J

Jay Dee

Is it possible to draw directory to the surface of a windows handle?
As in the following code example accesses the raw byte data of an image and manipulates it.

I understand how to manipulate images of different pixel formats etc in this way but I carnet work out how to obtain a reference to the equivalent Scan0 pointer of a window. I am assuming it is possible. The Graphics class must do it somehow.

Many thanks in advance.


var boundsRect = new System.Drawing.Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height);
System.Drawing.Imaging.BitmapData bitmapData = this.bitmap.LockBits(boundsRect, System.Drawing.Imaging.ImageLockMode.WriteOnly, this.bitmap.PixelFormat);
byte* pBase = (byte*)bitmapData.Scan0.ToPointer();

int y2 = (y * stride);
byte* pixel = (byte*)( pBase + y2 + x);
*pixel = color;

bitmap.UnlockBits(bitmapData);
bitmapData = null;
 
J

Jay Dee

Thank you for your reply, I guess DirectDraw or Direct2D are the way to go. I do not need a more detailed explanation, I do not really have a direct problem I need to address, I am just learning.
Basically I created my own custom Graphics class that Transformed/Scaled/Rotated a point and drew to a bitmap in a similar way that the Graphics class dose. Purely to gain an understanding of the mathematics of matrixes and the like.
I appreciate your input, thanks.
 
J

Jay Dee

I will defiantly consider looking into doing so, it probably wouldn’t be two hard once I got going as I understand more lo level logic than managed csharp anyway from working with assembly programming PIC microchips and thelike. My graphics implementation was purely a means two understanding the logic more than the code but it turned out to be very useful for mapping mouse coordinates to mach lines and other such shapes drawn to the screen on an angle. I used it two create an application for designing printed circuited boards by drag/dropping components and tracks onto a basic canvas and generating the machine coordinates to drive my CNC miller to cut out the boards, basically because I was too stingy to by the license for "Eagle" so I just wrote the functionality I needed from it myself. Looking back at the three weeks it took me two write the code it would have been cheaper two justpay for a copy of eagle. But I am a strong believer that understanding howsomething dose what it does is 10 times more important than being able to use other peoples tools to complete the task. Leaving the 3 weeks work worth every penny as knowledge.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top