Drawing in a picture box
Drawing in a picture box
Drawing in a picture box
Drawing in a picture box
Drawing in a picture box
Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box
Drawing in a picture box Drawing in a picture box
Drawing in a picture box
Go Back  Xtreme Visual Basic Talk > > > Drawing in a picture box


Reply
 
Thread Tools Display Modes
  #1  
Old 10-01-2012, 07:12 PM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default Drawing in a picture box


Is it possible to draw in a picture box, simple things like rectangles and circles?
Reply With Quote
  #2  
Old 10-02-2012, 02:58 AM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default Drawing in picturebox

Quote:
Is it possible to draw in a picture box, simple things like rectangles and circles?
Of course!
Draw Circle (the actual method used is "DrawEllipse")
Draw Rectangle page (using various ways)
vbHelper page: Draw Shapes

Note: Using "Dim g As Graphics = e.Graphics" can save you some typing (see code snippet below).
It's preferred to use "e.Graphics" over using "CreateGraphics" (although the "CreateGraphics" approach is pretty widely used,
if you search the Internet for graphics code samples, but I don't recommend it).
I also prefer using HighQuality for the "Drawing2D.SmoothingMode" (over AntiAlias used by the vbHelper code).

You can even:
BobPowell site: How to draw a rounded rectangle

vbHelper site: Draw Polygons

When you are ready to move on to some more advanced stuff:
Reshaping graphicspath alpha tint filled shapes using bezier curves and drag handles at runtime
Rounded rectangle shape with pathgradentbrush fuzzy drop shadowing

Code snippet (adapted from the vbHelper DrawShapes code cited above):
Code:
  Private Sub PictureBox1_Paint(sender As Object, _
      e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    Dim x As Integer = 10, y As Integer = 10
    Dim wid As Integer = 150, hgt As Integer = 75
    Dim g As Graphics = e.Graphics
    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
    g.FillRectangle(Brushes.AliceBlue, x, y, wid, hgt)
    g.DrawRectangle(Pens.Black, x, y, wid, hgt)
    y += hgt + 10
    g.FillEllipse(Brushes.LightGoldenrodYellow, x, y, wid, hgt)
    g.DrawEllipse(Pens.Black, x, y, wid, hgt)
    y += hgt + 10
    g.DrawLine(Pens.Black, x, y, x + wid, y + hgt)

    y = 10
    x += wid + 10
    g.DrawArc(Pens.Black, x, y, wid, hgt, -30, 270)
    y += hgt + 10
    g.DrawPie(Pens.Black, x, y, wid, hgt, -30, 270)
    y += hgt + 10

    Dim big_font As New Font("Comic Sans MS", 60, FontStyle.Bold, GraphicsUnit.Pixel)
    g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
    g.DrawString("Hello!", big_font, Brushes.Black, x, y)
  End Sub
Attached Files
File Type: zip draw_shapes_in_picturebox.zip (10.2 KB, 74 views)

Last edited by surfR2911; 10-02-2012 at 03:56 AM.
Reply With Quote
  #3  
Old 10-02-2012, 08:01 AM
AtmaWeapon's Avatar
AtmaWeaponDrawing in a picture box AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

surfR2911's post is informative; I want to add a little.

Consider not bothering with a PictureBox if you don't need to. I often find people asking how to draw on one are asking because that was the only way to do custom drawing in VB6 and they asume it's still true in VB .NET. You can draw on a PictureBox in VB .NET, but often it makes more sense to pick something like Control as the basis for your drawing. I no longer know if it hurts to use a PictureBox, but the rule of thumb "Why use it if you don't need it?" does apply.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 10-02-2012, 02:04 PM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default

Thank you, as for drawing on a control, I would be lost. I will play with the picturebox for now, but if you have any suggestions, for a better way, let me know, or if you have a small example, on drawing on a control, I would greatly appreciate it. I would need to draw several shapes, on the control though.

Just to let you know what I am trying to accomplish, is to create a program, that will draw a part, called a punch retainer, from user inputs, but at the same time, it will draw the outside shape, and all of the holes, to be machined in it. I've used vb6 for years, but never saw the need for a graphics window, to show the shape being created, but I need to do it with this, since it will be much more complicated.
Reply With Quote
  #5  
Old 10-02-2012, 02:14 PM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default

Oh sorry, nice example, i'll play with it when i get home, i will learn alot from this. I was doing this on the form, but that won't work, and i wasn't sure, how to get going on the picture box.

One last thing, is there a way to designate the center of the picturebox, as X0.0,Y0.0, so all of the users inputs can from that datum?
Reply With Quote
  #6  
Old 10-02-2012, 04:29 PM
passel's Avatar
passelDrawing in a picture box passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Use TransformTranslate to change the coordinate system.
There are probably a lot of examples on the forum, but the ones from this thread might be some of the simpler ones, drawing clock faces.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #7  
Old 10-02-2012, 05:33 PM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default

Something like this;
e.Graphics.TranslateTransform(CSng(Me.PictureBox1.Width / 2), CSng(Me.PictureBox1.Height / 2))
Reply With Quote
  #8  
Old 10-02-2012, 07:23 PM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default

Additional help, please
When I run this code, y1 is in the x,-y quadrant, it's reversed, any ideas???

******************************************************************
Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Dim x As Integer = 0
Dim y As Integer = 0
Dim x1 As Integer = 145
Dim y1 As Integer = 100
Dim g As Graphics = e.Graphics

e.Graphics.TranslateTransform(CSng(Me.PictureBox1.Width / 2), CSng(Me.PictureBox1.Height / 2))

g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality


g.DrawLine(Pens.White, x, y, x1, y1)


End Sub
Reply With Quote
  #9  
Old 10-02-2012, 07:29 PM
supercrewed supercrewed is offline
Centurion
 
Join Date: Feb 2004
Posts: 152
Default

Nevermind, found it;

Dim mx As New Drawing2D.Matrix(1, 0, 0, -1, 0, 0) 'Yaxis orientation flipped to match Cartesian plane
mx.Translate(0, -PictureBox1.Height) 'Move 0,0 to lower left corner
g.Transform = mx
Reply With Quote
  #10  
Old 10-16-2012, 05:35 PM
boops boops's Avatar
boops boops boops boops is offline
Centurion
 
Join Date: Dec 2006
Location: Holland and France
Posts: 146
Default

Quote:
Originally Posted by AtmaWeapon View Post
surfR2911's post is informative; I want to add a little.

Consider not bothering with a PictureBox if you don't need to. I often find people asking how to draw on one are asking because that was the only way to do custom drawing in VB6 and they asume it's still true in VB .NET. You can draw on a PictureBox in VB .NET, but often it makes more sense to pick something like Control as the basis for your drawing. I no longer know if it hurts to use a PictureBox, but the rule of thumb "Why use it if you don't need it?" does apply.
Hi Atma, a PictureBox does have a few advantages for people drawing with GDI+ methods, especially beginners:
1. it is available in the Toolbox.
2. it has a BorderStyle property.
3. it is double buffered.
4. it can have a transparent background.

These properties aren't necessary in every program but they are there when you need them. A Windows.Forms.Control can be modified, with varying degrees of technical difficulty, to have all these properties. But why? What advantage does that offer?

BB
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Drawing in a picture box
Drawing in a picture box
Drawing in a picture box Drawing in a picture box
Drawing in a picture box
Drawing in a picture box
Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box Drawing in a picture box
Drawing in a picture box
Drawing in a picture box
 
Drawing in a picture box
Drawing in a picture box
 
-->