Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,926 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,911 people online right now. Registration is fast and FREE... Join Now!




Drawing on form

 
Reply to this topicStart new topic

Drawing on form, Bad Idea

s_jsstevens
12 Dec, 2007 - 11:43 AM
Post #1

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 10


My Contributions
I was writing a small program, and I just ran into a stupid problem. I had drawn my image on the form so whenever I resized my form, it would destroy part of the image. How can I get around this problem?
CODE

    If load_flag = False Then
            Do While runcount > 0
                myBitmap = get_image(myBitmap, ctr1)

                drawX = myarray(ctr1, 4) * 24
                drawY = myarray(ctr1, 5) * 24

                myGraphics = Graphics.FromHwnd(ActiveForm().Handle)

                Try
                    myGraphics.DrawImage(myBitmap, drawY, drawX, 25, 25)
                Catch ex As Exception
                    MessageBox.Show("Error loading file" & runcount)
                End Try

                runcount -= 1
                ctr1 += 1
            Loop
            load_flag = True
        End If



Thank you in advnace

This post has been edited by s_jsstevens: 12 Dec, 2007 - 11:44 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Drawing On Form
12 Dec, 2007 - 11:59 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,280



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Put your code in the Paint event on the form:
CODE

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint


As an extra added bonus, the PaintEventArgs have the Graphics handle you're using already there. Paint should be called everytime the form need to be redrawn. There are some nuances to this, where you can figure out just what needs to be repainted, but for the most part you can just do the whole thing.

Note, for extra speed, hang on to that bitmap and just flash that to the screen. If the image is static, consider just putting it in a picturebox control and letting vb do the work for you.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

s_jsstevens
RE: Drawing On Form
12 Dec, 2007 - 01:22 PM
Post #3

New D.I.C Head
*

Joined: 4 Dec, 2007
Posts: 10


My Contributions
Thank you, but that isnt exactly what I needed. Is there any way I can instead of drawing to the form. I can draw it into the forms background image?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Drawing On Form
12 Dec, 2007 - 01:37 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
You could simply set the BackgroundImage property of the Form to the image you want and then set the BackgroundImageLayout to Stretch. This will cause the form to redraw the image for you to fit the size of the form.

This will come at a cost, though. You will see a flickering effect as the form is resized and the image will be stretched out of proportion to match the form size.

You could minimize the flickering problem by drawing the image on the form yourself, as you are currently doing, but only draw on the form from inside the ResizeEnd event of the form. This event is raised after the user has completed resizing the form.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month