[RESOLVED] Get the image generated by Graphics into a bitmap
Inside PictureBox1_Paint I used PaintEventArgs parameter to draw some graphics and make transformations (rotation/scale).
All changes are visible in the picturebox. Now I want to extract the image/bitmap updated, I mean, a bitmap with all that transformations I did, how to do it? The image in the picturebox is always Nothing.
VB Code:
Private Sub PictureBox1_Paint(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.Clear(PictureBox1.BackColor)
RotateImage(e) 'Another sub
ZoomImage(e) 'Another sub
e.Graphics.DrawImage(MyBitmap, -PosX, -PosY) 'My bitmap drawn with transformation
End Sub
There I send MyBitmap to be drawn by e.Graphics, but it's not 'transformed', it's just used to draw, How can I get the transfomed bitmap?
Re: From graphics created with PaintEventArgs to real image
from the look of your code (without seeing the other routines source code)
it looks like your modified image is in the MyBitmap variable when you are at this line of code, correct?
VB Code:
e.Graphics.DrawImage(MyBitmap, -PosX, -PosY) 'My bitmap drawn with transformation
so you can just use that bitmap object. Unless I am missing something.
Also, the reason the image in the picturebox is nothing, is because you are drawing the image directly to the DC of the picturebox, which is different than assigning an image property to it.
Re: From graphics created with PaintEventArgs to real image
Nope, the MyBitmap is never updated, e.Graphics has all the transformations, anything you send to it will be drawn using that transformations, a line, a rectanlge, or a bitmap, but the bitmap is never updated, it's never transformed, it's just used to draw, but the transformations are not reflected in the bitmap.
Also, the reason the image in the picturebox is nothing, is because you are drawing the image directly to the DC of the picturebox, which is different than assigning an image property to it.
Ok, then, is there any way to create a bitmap from that DC?
Re: From graphics created with PaintEventArgs to real image
Bump.
I just need to extract that transformed bitmap from there. The transformation is visible (rotation/scale), but I just can't find the way to extract that new image generated by System.Drawing.Graphics into a new bitmap.
At least, anybody here knows how to get the DC of a picturebox?? This way, maybe I could use BitBlt or StrechBlt to get that image.
Re: Get the image generated by Graphics into a bitmap
Ok, then, that would be FromDc parameter for BitBlt, now I need DestDc, but VB.net Pictureboxes and bitmaps don't have hDC, like in VB6, at least i can't find it. Maybe I could use CreateCompatibleDC API but, I just can't believe that this stuff is not native in VB.net, weird.
Re: Get the image generated by Graphics into a bitmap
This exercise has forced me to learn a lot about the graphics object and graphics transformations.
The way I handle this problem is to create a module-level graphics object which will handle all of our drawing. We can at any time access the contents of the grapics object through it's associated bitmap. We need two module-level bitmaps: one to hold the original picture which we will continually redraw to the graphics object; and the other to store the pixel data of the graphics object.
Note that all the transformations are cumulative. This means that if we want to rotate by a certain amount, we have to know how much we've rotated already. Same goes for scaling. Here are the new functions for rotation and scaling.
Notice that in the TrackZoom Scroll we have to form the ratio of the previous zoom position to the new zoom position to get the new zoom factor. In the case rotation it is easier since rotation is additive.
Finally, if you want to access the current image simply access the associated bitmap.
Re: Get the image generated by Graphics into a bitmap
Moeur, thank you from the heart man, that works perfectly
As you said, transformations are cumulative that was not the case in my old code, I was using the Paint event of the picturebox sending always the original bitmap, it was transformed but temporary, and there was not way to save it. Now i see how transformations work. Thank you again!