Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

storing graphics in form of Bitmap

Posted on 2005-04-28
13
Medium Priority
?
7,161 Views
Last Modified: 2013-12-03
Hi all. I draw lines in my pictureBox in the paint event...:
        e.Graphics.DrawLine(x1,y1,x2,y2)
Now, I want to make a bitmap of the image of the pictureBox OR basically a bitmap displaying my lines. This way I can manage to rotate, stretch, print, and save it.(I know that I do not need a bitmap, but it would make everything much simpler...)
How can I do something like:   bitmap1.darwLine(...)    ?
and how to save the graphic? the image property of pictureBox stays empty !
Look. I have already tried to retrieve the hdc and it works, but it captures any form lying over the pictureBox and ruines my picture:    
'==========the following cannot possibly work correctly:
        ' Make a Bitmap to hold the image.
        Dim bm As New Bitmap(PictureBoxGraph.Width, PictureBoxGraph.Height, me_gr)
        Dim bm_gr As Graphics = me_gr.FromImage(bm)
        Dim bm_hdc As IntPtr = bm_gr.GetHdc

        ' Get the PictureBoxGraph's hDC. We must do this after
        ' creating the new Bitmap, which uses me_gr.
        Dim me_hdc As IntPtr = me_gr.GetHdc

        ' BitBlt the PictureBoxGraph's image onto the Bitmap.
        BitBlt(bm_hdc, 0, 0, PictureBoxGraph.ClientSize.Width, PictureBoxGraph.ClientSize.Height, _
            me_hdc, 0, 0, SRCCOPY)
        me_gr.ReleaseHdc(me_hdc)
        bm_gr.ReleaseHdc(bm_hdc)
0
Comment
Question by:Dan-Key
  • 7
  • 6
13 Comments
 
LVL 9

Accepted Solution

by:
Lacutah earned 1000 total points
ID: 13890399
Create all your graphics to a bitmap, make sure the bitmap is referenced in your form class, and whenever you want to alter/rotate use that cached bitmap.

In your form's declarations:
       dim bmp as Bitmap()

In your Paint Event:

        bmp = New Bitmap(PictureBoxGraph.Width, PictureBoxGraph.Height) 'Create new bitmap same size of your picturebox
        Dim g As Graphics = Graphics.FromImage(bmp) 'Get the graphics object for the bitmap
        g.DrawLine(Pens.Black, 1, 1, 5, 5) 'Draw using the Bitmaps Graphics object
        e.graphics.DrawImage(bmp, 0, 0) 'Paint the bitmap to the paint events graphics object.
0
 
LVL 9

Expert Comment

by:Lacutah
ID: 13890413
Where's the grammer checker when I need one...

To complete the first sentence:
   ...and whenever you want to alter/rotate use that cached bitmap.
Use:
   ...and whenever you want to alter/rotate/save/ or otherwise use that image, simply reference the bitmap object.
0
 

Author Comment

by:Dan-Key
ID: 13890962
Thanks for the comment.
Another problem arised by that is this:
I draw the lines in the paint event by calling a class using an interface...
How can I have something like the following in the paint event?

    Private Sub PictureBox1(ByVal sender As Object, ByVal e As     System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
      '''Drawing many things on e.Graphics
      bmp.DrawGraphics(e.graphics) 'make my bitmap from the graphics object
   End sub
'==================
'This is a kind of reverse of your code
Dim g As Graphics = Graphics.FromImage(bmp) 'Get the graphics object for the bitmap
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Lacutah
ID: 13891734
Does you class accept a graphics object, or is it expecting a picturebox object?
0
 

Author Comment

by:Dan-Key
ID: 13892125
It expects a pictureBox. I think that I either have to make another class to this or get rid of complex interfaces...which is so much extra work.

I cannot replace the expected pictureBox in my interface with a graphics object, because I want to use the size of the pictureBox to stretch image when resizing...

I appreciate any related comments
0
 
LVL 9

Expert Comment

by:Lacutah
ID: 13892137
Why not make your interface more useful by asking for (as parameters):
1.) Width in pixels
2.) Height in pixels
and
3.) a Graphics object?

And to maintain even more flexability, ask for startx and starty parameters (upper-left-corner) cordinates?  Overload the interface to assume point(0,0) as default start corner...
0
 
LVL 9

Expert Comment

by:Lacutah
ID: 13892141
When I say "more useful", I mean more flexiable.  Tying a control to an interface JUST because of a specific controls width & height seems antagonistic - you might want to do this in the future by overriding a buttons on-paint method, where the image should conform to the inside (not the button border) of the object's graphics surface.
0
 

Author Comment

by:Dan-Key
ID: 13901826
I cannot agree with that. If you send an integer parameter picture.Height it stays in the memory as a constant number, so when you resize the pictureBox, the paint event uses the old Height and does not stretch my image...
I should decide on a good methodology...

Another question you can help me with:
to draw a rectangle selection on the MouseMove event of a pictureBox I need to know how to do it:
using
        ControlPaint.DrawReversibleFrame(rec, Color.Black, FrameStyle.Dashed)

Note that I do not want to call the paint event during MouseMove (it works very very slowly)
0
 

Author Comment

by:Dan-Key
ID: 13902914
I figured out. Thanks
0
 

Author Comment

by:Dan-Key
ID: 13906309
Lacutah, about your code:
Dim g As Graphics = Graphics.FromImage(bmp)

When you change g, you then change bmp.
How come it works from right to left and refers bmp to g  ?
0
 
LVL 9

Expert Comment

by:Lacutah
ID: 13906614
Dim g As Graphics = Graphics.FromImage(bmp) allows you to paint to the bitmap - after this statement the graphics object references (alters) the bitmap image.

"How come it works from right to left and refers bmp to g  ?"  I'm not understanding this question?
0
 

Author Comment

by:Dan-Key
ID: 13912702
I'm sorry for my vagueness.
We know that g is a reference to a Graphics object, containing a bitmap. When we change the reference, we actually change the object. I just understood that the method "FromImage" has the ability to apply changes in Graphics object to an initilized bitmap.

Well, I'm a new VB.NET programmer.
0
 
LVL 9

Expert Comment

by:Lacutah
ID: 13915401
To quote from help:

"The Graphics class provides methods for drawing objects to the display device. A Graphics object is associated with a specific device context."

the Graphics.FromImage() is a shared method of the graphics object - it creates a new graphics object that associates with an image.

" When we change the reference, we actually change the object.", this is incorrect, we changed the object pointer of g (which was uninitialized - pointing to nothing) to point to a new graphics object created by the shared mehod "FromImage()"

For example:

dim g as Graphics 'g = nothing...
g = graphics.fromimage(SomeImage#1) 'g now references a new graphics object, I'll call it the "first object"
g = graphics.fromimage(SomeImage#2) 'g has had it's reference reassigned to another graphics object, the "first object" is now out of scope and is now a candidate for garbage collection.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question