Two Graphics Questions

PatL

Member
Joined
May 15, 2009
Messages
7
Programming Experience
Beginner
After several years of resisting, I've finally been pushed form VB 6 into .NET. And I am kicking and screaming all the way thank you.

So, what I had before: Using VB 6 I developed an electronics cooling application. The code has a lot of translation routines (turning geometry options into nodes and resistors) and a bit of graphics (so users can see the board layouts and PWB stackups.)

What I have now: The port to VS 2005 when fairly smoothly given the size of the code and the fact that I am a mechanical engineer and not a software guy. But two (related) problems are frustrating me to no end. I have a handful of pictureboxes that I am drawing rectangles, lines, and strings to. That part seems to work fine.

What isn't working is the old "AutoRefresh". When I go to create the form with the picturebox and all the drawing entities, it first showed up blank. I figured out I need to do all the drawing in the paint event with a "g As Graphics = Me.Pic1.CreateGraphics" statement. But still, when the form first pops up, it is blank. Actually, you can see the picturebox getting populated and then it quickly blanks out. If I move another form over the top of it, then it redraws. Also, if I min/max the form, the image is erased.

Also, I'm trying to write the image in the picturebox to a file. Should be just a simple matter of "Pic1.Image.Save("c:\My Data\test.bmp")". But the error message says the Pic1.image is "Nothing" (emtpy, nada, blank, zippo, etc).

How can it be "Nothing" when I see the results of all my drawing efforts? I have even put in a test statement like "if Pic1.Image is nothing then....." right after a do a g.fillrectangle and it says that it is empty.

Keeping in mind that I just jumped into VB .NET, ca anybody explain to me what I am missing?

Thanks in advance

Pat
 
After several years of resisting, I've finally been pushed form VB 6 into .NET. And I am kicking and screaming all the way thank you.

Lots of us (including me) have been there. Give it a couple of months, and you'll wonder how you ever managed with VB6 :)


How can it be "Nothing" when I see the results of all my drawing efforts? I have even put in a test statement like "if Pic1.Image is nothing then....." right after a do a g.fillrectangle and it says that it is empty.

It's empty because what you draw on it is not saved to the Image. The best way to describe it is that any drawing that you do is "write-only". Apart from taking a screenshot, you cant "read the drawing" that you did.

I'll list the most useful things I found when I migrated from VB6 :-

1. Learn about Invalidate.

2. Learn about TranslateTransform, ScaleTransform and Matrix (I cant begin to think how much time this saved :D)

3. Put all your drawing routines into separate functions, and pass Graphics to those functions by reference. You can then re-use the drawing routines depending on whether you are writing to the screen, a bitmap, printing etc etc

Hope that helps - and if it doesnt, coffee will :D
 
Okay, I think I understand. What I am drawing via "g.fillRectangle" and such commands is really only going to the screen, and not to the PictureBox Image object. And what I should be doing is drawing everything to a virtual image.

Then when I want to view the image, I set the picturebox image to that virtual image. Or I can print that virtual image. Or write it to a file.

Correct?

So I need to initiate a global image object for each form. Then point all my drawing routines to that. Right?

Got an example/hint of how to do that?

Thanks again.

Pat

PS: What if I don't like coffee?:D
 
Okay, I think I understand. What I am drawing via "g.fillRectangle" and such commands is really only going to the screen, and not to the PictureBox Image object.

Correct so far.

And what I should be doing is drawing everything to a virtual image.

Then when I want to view the image, I set the picturebox image to that virtual image. Or I can print that virtual image. Or write it to a file.

If you want to do it that way. It isnt necessarily correct or incorrect. My point really was that you should write the drawing routines into their own functions so they can be reused.

An example as requested :-

VB.NET:
Option Explicit On
Option Strict On
 
Public Class Form1
 
    Public Sub New()
 
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.UserPaint, True)
 
    End Sub
 

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'put a resistor picture on the main form
        ShowAResistor(e.Graphics)
    End Sub
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bmpResistor As New Bitmap(200, 200)
        Dim g As Graphics = Graphics.FromImage(bmpResistor)
        'the same resistor we did earlier, but this time for saving to a file
        ShowAResistor(g)
        bmpResistor.Save("c:\temp.bmp")
    End Sub

    Private Sub ShowAResistor(ByVal eg As Graphics)
        With eg
            'only one resistor in here, but used two different ways
            'this code could also be used for printing
            .DrawRectangle(Pens.Black, 50, 50, 100, 25)
        End With
    End Sub

End Class



PS: What if I don't like coffee?:D

Learn, my friend :D
 
Following your example, which made sense, I am now able to see my layout and save the file. Many thanks.

And I don't have to learn how to drink coffee either.
 

Latest posts

Back
Top