banner



How To A Drawing Picturebox Vb.net

  1. #i

    Goshx is offline

    Thread Starter

    Fanatic Member


    Drawing inside od picturebox

    Here'southward code which draws freehand lines inside of movie box. My goal is to enable cartoon over an image placed inside of Picturebox command. This code tin can't exercise that merely firstly clear existing content from the picturebox

    Code:

                                      Private memimg As New Bitmap(800, 800)    Private canvass Equally Graphics = Graphics.FromImage(memimg)    Individual penCol Every bit Color        Private Sub PicBox1_MouseDown(ByVal sender Equally Organization.Object, ByVal e As Organisation.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseDown         bMouseDwn = True         startX = east.X         startY = due east.Y     End Sub      Private Sub PicBox1_MouseUp(ByVal sender As System.Object, ByVal e As Organization.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseUp          endX = e.X         endY = eastward.Y          Me.PicBox1.Image = memimg         penCol = Color.Red         PicBox1.Refresh()          bMouseDwn = False      End Sub       Private Sub PicBox1_MouseMove(ByVal sender As System.Object, ByVal east As Organisation.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseMove          Dim cPen As New Pen(Color.Black, 3)          If bMouseDwn Then             cPen.Color = penCol             sheet.DrawLine(cPen, startX, startY, e.10, e.Y)             startX = e.X             startY = eastward.Y             Me.PicBox1.Prototype = memimg         End If         cPen.Dispose()     End Sub

  2. #ii

    Re: Drawing inside od picturebox

    What you should do is non create a new Bitmap but use the existing bitmap from your PictureBox control. You wouldn't need to reset the Image property either.

  3. #3

    Re: Drawing inside od picturebox

    Perhaps you want to describe "over" the prototype, not on the epitome, so instead of drawing to the existing bitmap from the PictureBox control, describe on your memory bitmap, then draw the retentivity bitmap onto your picturebox in the Paint event.

    This allows y'all to draw "over" your image, and you can erase the cartoon by simply not drawing it, or modify it, erase portions, etc...
    For an case, I just modified your code slightly to add the Pigment Issue handler, and remove the bitmap consignment to the picturebox.
    Also y'all tin Erase the overlay cartoon by clicking the right mouse button. The bitmap is reinitialized to a new bitmap (which is transparent by default), thus erasing the current drawing overlay and assuasive you lot start a new drawing overlay.

    p.south. Since you didn't have a number of variables included in the code you posted, I added them here. If they already exist elsewhere in your code, delete the 2 lines I added to the declarations area for startX, startY, etc...

    Code:

                                    Private memimg As New Bitmap(800, 800)   Individual canvass As Graphics = Graphics.FromImage(memimg)   Individual penCol As Color = Color.Blood-red   Individual startX, startY, endX, endY As Integer   Private bMouseDwn As Boolean    Individual Sub PicBox1_MouseDown(ByVal sender As System.Object, ByVal eastward As System.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseDown     If e.Button = Windows.Forms.MouseButtons.Right And then    'Right Push button, articulate the existing overlay       sheet.Dispose()       memimg.Dispose()       memimg = New Bitmap(800, 800)       sheet = Graphics.FromImage(memimg)     Terminate If     bMouseDwn = Truthful     startX = east.X     startY = e.Y   End Sub    Private Sub PicBox1_MouseUp(ByVal sender As Arrangement.Object, ByVal e As Organization.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseUp      endX = e.X     endY = e.Y      penCol = Color.Red     PicBox1.Refresh()      bMouseDwn = False    End Sub     Private Sub PicBox1_MouseMove(ByVal sender As Arrangement.Object, ByVal eastward As System.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseMove      Dim cPen Every bit New Pen(Color.Black, 3)      If bMouseDwn Then       cPen.Colour = penCol       sheet.DrawLine(cPen, startX, startY, e.X, due east.Y)       startX = eastward.X       startY = e.Y       PicBox1.Invalidate()     End If     cPen.Dispose()   End Sub    Private Sub PicBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PicBox1.Paint     e.Graphics.DrawImage(memimg, 0, 0)   End Sub

  4. #4

    Goshx is offline

    Thread Starter

    Fanatic Member


    Re: Cartoon inside od picturebox

    Quote Originally Posted by dday9 View Post

    What you should do is not create a new Bitmap merely apply the existing bitmap from your PictureBox command. Y'all wouldn't demand to reset the Paradigm property either.

    I've tried earlier sending my question here. I couldn't reach that

  5. #5

    Goshx is offline

    Thread Starter

    Fanatic Member


    Re: Drawing inside od picturebox

    Quote Originally Posted by passel View Post

    Perhaps you desire to depict "over" the image, not on the image, and then instead of drawing to the existing bitmap from the PictureBox control, describe on your retentivity bitmap, so draw the retentiveness bitmap onto your picturebox in the Pigment event.

    Unfortunately, no. Information technology is a zoomable pic box so we can't practise that with "over" approach It should be done inside of moving-picture show box bitmap or with a memory bitmap which should exist copied before every mousewheel event. That should exist more than complicated IMHO.

  6. #6

    Re: Drawing inside od picturebox

    What dday9 is saying is to depict straight on the epitome of the picturebox, then yous would need to become a graphics object from that image.
    Of course, we are assuming you practise have an image loaded in the picturebox.
    It is probably best to simply get the graphics object and dispose of it (here using the Using End Using block to create and dispose of information technology) equally needed. It is simpler than the overlay, but permanently changes the prototype. To erase the "drawing", yous take to reload the original epitome in the picturebox.

    Code:

                                      Individual penCol As Color = Color.Cherry-red   Private startX, startY, endX, endY Equally Integer   Individual bMouseDwn As Boolean    Private Sub PicBox1_MouseDown(ByVal sender As Arrangement.Object, ByVal due east As Arrangement.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseDown     bMouseDwn = True     startX = e.X     startY = e.Y   End Sub    Private Sub PicBox1_MouseUp(ByVal sender Every bit System.Object, ByVal e As Arrangement.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseUp      endX = e.X     endY = due east.Y      penCol = Color.Reddish     PicBox1.Refresh()      bMouseDwn = False    End Sub     Private Sub PicBox1_MouseMove(ByVal sender As System.Object, ByVal due east As System.Windows.Forms.MouseEventArgs) Handles PicBox1.MouseMove      Dim cPen As New Pen(Color.Black, three)      If bMouseDwn Then       Using canvas Equally Graphics = Graphics.FromImage(PicBox1.Image)         cPen.Colour = penCol         sheet.DrawLine(cPen, startX, startY, e.X, due east.Y)         startX = east.10         startY = due east.Y       Stop Using       PicBox1.Invalidate()     End If     cPen.Dispose()   End Sub
    p.s. And every bit you posted as I was writing this, appears to exist desire you want.
    p.p.s. But I do wonder how yous're zooming the picturebox. The complicated part would non exist the zooming of the drawing, merely the zooming of the mouse coordinates to proceed the mouse movement positions consitent with the scale of your paradigm.
    Last edited by passel; Jan 11th, 2016 at 04:09 PM.

  7. #7

    Goshx is offline

    Thread Starter

    Fanatic Member


    Re: Drawing inside od picturebox

    This seems to piece of work fine merely I've realized that there's another problem. Namely, this pic box isn't only zoomable but likewise it is movable. That's the reason why I can't describe what I want. At present I take to retrieve how to stop moving before of drawing within of pic box.

    You asked me how I'thou zooming picturebox? Information technology is a simple free control which I've found in net. It works its job quite fine simply it lacks many features which could be useful. I'1000 not an good for custom controls to extend its capabilities


  8. #8

    Re: Drawing inside od picturebox

    If yous aren't wedded to the zoomable film box yous accept found, you might like to try my ZoomPictureBox in the CodeBank (link in signature below). Possibly the demo code in the thread already does something similar you want to do. If non, I'm sure I could assistance you lot a bit with it.

    BB


  9. #9

    Goshx is offline

    Thread Starter

    Fanatic Fellow member


    Re: Drawing inside od picturebox

    I\m not sure is that your control which I'one thousand using or some very similar to your control from another author.
    I've just found ypur tread a few hours ago.

    http://www.vbforums.com/showthread.p...entred-zooming

    It could exist good to add some additional properties at that place. In any case, a squeamish job.


  10. #10

    Re: Drawing inside od picturebox

    Quote Originally Posted by Goshx View Post

    I\one thousand non certain is that your command which I'thousand using or some very like to your control from another writer.
    I've just found ypur tread a few hours ago.

    http://www.vbforums.com/showthread.p...entred-zooming

    It could be practiced to add together some additional properties there. In whatsoever case, a prissy job.

    Cheers. I already (half) promised to add a PointToImage function, which returns the current mouse coordinates in Paradigm coordinates. And that's what you lot need for drawing on the zoomed epitome. Some time back, I coded the function (simple enough) and drafted a demo program which does exactly that: freehand drawing on the zoomed/panned image. And and so I forgot all almost it. Now I've rediscovered it and here it is.

    For the moment the PointToImage function is on the test class, just sooner or after I'll update the ZoomPictureBox itself to include it. To endeavor it out, brand a Form1 with a ZoomPictureBox1, a Button1 and a Label1 e.thousand. as below. Use the Spacebar to switch betwixt cartoon and image panning.

    Name:  DrawOnZPB.jpg  Views: 996  Size:  37.2 KB

    Here's the code for the form

    Code:

    Public Class Form1      Individual img As Image     Individual prevPoint As Point     Private drawingMode As Boolean = True      Individual Sub Form1_Load(sender Every bit Object, e As EventArgs) Handles MyBase.Load         img = New Bitmap(600, 600)         Using m Equally Graphics = Graphics.FromImage(img)             thou.Clear(Color.White)         End Using         ZoomPictureBox1.EnableMouseDragging = Fake                                  'start in Drawing mode                                ZoomPictureBox1.Paradigm = img     End Sub                                'Office to become the Image pixel respective to the mouse position:                                Individual Part PointToImage(ZPB As ZoomPictureBox, p As PointF) As Point         Dim 10, y As Integer         With ZPB             x = CInt((p.Ten - .ImagePosition.10) / .ZoomFactor)             y = CInt((p.Y - .ImagePosition.Y) / .ZoomFactor)         Cease With         Return New Point(10, y)     End Part                                'Toggle panning/drawing by hitting the space bar:                                Private Sub ZoomPictureBox1_KeyPress(sender As Object, eastward Every bit KeyPressEventArgs) Handles ZoomPictureBox1.KeyPress         If due east.KeyChar = " " And then             drawingMode = Non drawingMode             If drawingMode And so 'drawing:                 ZoomPictureBox1.EnableMouseDragging = False                 ZoomPictureBox1.Cursor = Cursors.Default                 Label1.Text = "Drawing Mode (Printing Space Bar to Toggle)"             Else 'panning                 ZoomPictureBox1.EnableMouseDragging = True                 ZoomPictureBox1.Cursor = Cursors.SizeAll                 Label1.Text = "Panning Mode (Press Space Bar to Toggle)"             End If         End If     End Sub                                                                  'Initiate drawing:                                Private Sub ZoomPictureBox1_MouseDown(sender Equally Object, e As MouseEventArgs) Handles ZoomPictureBox1.MouseDown         If e.Button = Windows.Forms.MouseButtons.Left And then prevPoint = e.Location     End Sub                                                                  'Draw on the image:                                Private Sub ZoomPictureBox1_MouseMove(sender As Object, eastward Every bit MouseEventArgs) Handles ZoomPictureBox1.MouseMove                                                                  'Add a line segment to the epitome:                                If drawingMode AndAlso due east.Push = Windows.Forms.MouseButtons.Left Then             Dim A Equally Bespeak = PointToImage(ZoomPictureBox1, prevPoint)             Dim B As Point = PointToImage(ZoomPictureBox1, e.Location)             Using m Every bit Graphics = Graphics.FromImage(img)                 yard.SmoothingMode = Drawing2D.SmoothingMode.HighQuality                 Using pn Equally New Pen(Color.Red, 3)                     one thousand.DrawLine(pn, A, B)                 End Using             Finish Using             ZoomPictureBox1.Invalidate()             prevPoint = e.Location         End If     Cease Sub      Private Sub Button1_Click(sender Equally Object, due east As EventArgs) Handles Button1.Click         Using ofd As New OpenFileDialog             If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then                 Effort                     img = Paradigm.FromFile(ofd.FileName)                     ZoomPictureBox1.Image = img                 Catch                     MessageBox.Show("Prototype Load Mistake")                 Stop Try             Terminate If         Finish Using     Cease Sub  End Form
    BB

  11. #11

    Goshx is offline

    Thread Starter

    Fanatic Fellow member


    Re: Drawing inside od picturebox

    I wanted to propose y'all a few things how to improve your zoomable moving-picture show box. I didn't try how your case works (previously I have to download and to try your demo) merely you could add a property which would lock motion-picture show for moving and zooming. This should enable drawing on visible area of picture while information technology is locked.

    Some other potentially very useful affair could be determining of relative position of zoomed image inside of picture show box. That values could be saved and used for restoring of previous position. I have no idea how to summate that (probably determining meridian-left position of image, its altitude from peak-left of picturbox and delta from mouse bicycle value) merely it would exist very squeamish to see.


  12. #12

    Re: Cartoon inside od picturebox

    Quote Originally Posted by Goshx View Post

    I wanted to propose y'all a few things how to improve your zoomable pic box. I didn't try how your example works (previously I have to download and to attempt your demo) but yous could add a property which would lock flick for moving and zooming. This should enable drawing on visible area of picture while it is locked.

    It already has those backdrop: EnableMouseDragging and EnableMouseWheelZooming.

    EnableMouseDragging=Imitation is used in the Load sub and the KeyPress sub in my previous mail to enable drawing. In that location'south no demand to disable zooming here. If you don't take these properties, y'all may be using an earlier version of my code (although these "properties" were already present as Public variables from the start). If necessary download the electric current version from Post #1 of my CodeBank thread; the demo zip includes the class, so at that place is no demand to download both zips.

    Some other potentially very useful thing could exist determining of relative position of zoomed image inside of picture box. That values could be saved and used for restoring of previous position. I have no thought how to summate that (probably determining elevation-left position of image, its altitude from top-left of picturbox and delta from mouse wheel value) just it would be very nice to encounter.

    It also already exists: the ImagePosition holding. You lot can utilize that together with the ZoomFactor property to salvage and restore the current image coordinates in your own program.

    BB

    Last edited by boops boops; Jan 12th, 2016 at 07:41 PM.

  13. #13

    Goshx is offline

    Thread Starter

    Fanatic Member


    Re: Drawing inside od picturebox

    I can see EnableMouseDragging and EnableMouseWheelZooming but there's no ImagePosition. Also, there's no any property related with zoom factor. How it could be possible?

  14. #14

    Re: Drawing within od picturebox

    So, in the code you blazon something like ZoomPictureBox1.I and didn't see ImagePosition listed in Intellisense?
    On other controls you lot should have noticed that not all backdrop are listed in the IDE Backdrop list, usually those that are calculated as office of the control and don't really apply every bit presets.

  15. #15

    Re: Cartoon inside od picturebox

    boops boops,
    I noticed while testing that sometimes when I was zoomed in and drawing near the bottom of the image, that the cartoon was offset quite aways from the mouse (always in Y in my case with several unlike images, but don't know if that would e'er be the case).

    I determined the reason for this is that the bounds (_ImageBounds.Width and Tiptop) ratios had drifted.
    The Width and Peak values were initially different in my images, simply if you zoomed mode out, then zoomed fashion in, because of the calculation of the width and height always being in reference to themselves (and not the original prototype) and limited to an Int (CInt), each would round to an Int at different points and eventually the Width and Peak would go square to be in phase with each other.

    So, I just modified those 2 lines where the Width and Tiptop of the bounds was calculated to fix that by referencing the original size of the paradigm. The other places where you use zoomRatio for recentering are fine as I presume in one case the bounds are correct, other calculations will take care of themselves.

    Code:

    'In ZoomPictureBox_Main.vb     'Calculate the prototype bounds for a given ZoomFactor,       Private Function GetZoomedBounds() As Rectangle          'Find the zoom center relative to the paradigm bounds.         Dim imageCenter As Bespeak = FindZoomCenter(_ZoomMode)          'Calculate the new size of the the image bounds.         _previousZoomfactor = _ImageBounds.Width / _Image.Width         If Math.Abs(_ZoomFactor - _previousZoomfactor) > 0.001 And then             Dim zoomRatio As Double = _ZoomFactor / _previousZoomfactor       '   _ImageBounds.Width = CInt(_ImageBounds.Width * zoomRatio)  'Fixed these two lines and so they don't accrue Integer rounding values       '   _ImageBounds.Height = CInt(_ImageBounds.Height * zoomRatio)             _ImageBounds.Width = CInt(_Image.Width * _ZoomFactor)                 _ImageBounds.Meridian = CInt(_Image.Summit * _ZoomFactor)              'Discover the resulting position of the zoom center prior to correction.             Dim newPRelative As Point             newPRelative.X = CInt(imageCenter.X * zoomRatio)             newPRelative.Y = CInt(imageCenter.Y * zoomRatio)              'Apply a correction to return the zoom center to its previous position.             _ImageBounds.X += imageCenter.10 - newPRelative.X             _ImageBounds.Y += imageCenter.Y - newPRelative.Y          Cease If         _previousZoomfactor = _ZoomFactor         Return _ImageBounds     End Function
    Terminal edited by passel; January 12th, 2016 at 07:53 PM.

  16. #sixteen

    Re: Drawing within od picturebox

    Quote Originally Posted by Goshx View Post

    I can run across EnableMouseDragging and EnableMouseWheelZooming but in that location's no ImagePosition. Also, there'southward no any holding related with zoom factor. How information technology could be possible?

    Sorry about that. The property'southward there, but you can't see information technology in the Designer or in Intellisense. It seems I made some errors when I uploaded the terminal version of the zpb code virtually 4 years agone. Since and then well-nigh 6,000 people have downloaded it, but no one has mentioned whatever problems; simply that'due south the manner it goes.

    There were 3 properties that I didn't want to expose in the designer: ZoomFactor, ImageLocation and ImageBounds. I marked them with the <Browsable("Faux")> attribute, which is widely suggested every bit a style to keep things out of the designer. But I didn't know at the time that this attribute too hides the backdrop from IntelliSense. At present I've discovered that the right attribute to employ is this:

    Lawmaking:

                                      <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
    Now y'all tin can run into the properties concerned in Intellisense. I've also stock-still a few other odds and ends that I noticed while trying to effigy out what was wrong. Use the attached vb files to replace the older versions and rebuild the projection. I'll post these to the CodeBank thread later on when I've had time to exam them meliorate, maybe with a some small enhancements.

    BB

    EDIT: if you desire to try the new version, download and replace

    both files beneath. They are two parts of the same Class (Partial Classes).
    Last edited by boops boops; Jan 13th, 2016 at 12:47 PM.

  17. #17

    Re: Drawing inside od picturebox

    Quote Originally Posted by passel View Post

    boops boops,
    I noticed while testing that sometimes when I was zoomed in and drawing near the lesser of the image, that the drawing was offset quite aways from the mouse (always in Y in my case with several unlike images, but don't know if that would always be the instance).

    I determined the reason for this is that the bounds (_ImageBounds.Width and Height) ratios had drifted.

    Thanks Passel! It occurred to me while looking through the code this evening that those lines could exist a source of cumulative error, but I haven't seen whatsoever effects yet. Your corrections are exactly what is needed! I'll put them in the next version in the CodeBank. BB

  18. #18

    Goshx is offline

    Thread Starter

    Fanatic Member


    Re: Drawing within od picturebox

    Quote Originally Posted by passel View Post

    So, in the code you blazon something like ZoomPictureBox1.I and didn't see ImagePosition listed in Intellisense?
    On other controls you should have noticed that not all backdrop are listed in the IDE Properties list, usually those that are calculated every bit part of the control and don't really utilize equally presets.

    I was talking about IDE properties panel. That's why I couldn't realize what BB wants to say

    Although information technology seems that I am using BB's pic box control, mine is 32KB on the opposite of 18KB from BB's demo project.


  19. #19

    Goshx is offline

    Thread Starter

    Fanatic Fellow member


    Re: Drawing inside od picturebox

    Error 1 '_imageInitialized' is not declared. It may be inaccessible due to its protection level. C:\Users\P2\Desktop\ZoomPictureBox+TestForm+Demo\ZPBlib\ZoomPictureBox_Public Properties.vb 40 5 ZPBlib

    An mistake subsequently adding of code suggested past BB, post #16.


  20. #20

    Re: Drawing inside od picturebox

    Yous demand to download and replace both files from post #sixteen, then rebuild. (When I said to download only 1, I was talking nearly the thread in the CodeBank.) BB

  21. #21

    Goshx is offline

    Thread Starter

    Fanatic Member


    Re: Drawing inside od picturebox

    I did exactly that bot it didn't work from some unknown reason. When I did that again, it started to work as expected ))

    Why "my" version of zoomable pic box has 32KB instead of 18KB? I don't have source code to check but proper noun of dll is the aforementioned.(ZPBLib). Information technology is a significant divergence in size.


  22. #22

    Re: Drawing within od picturebox

    Was one compiled for x64 or AnyCpu and the other for X86. x64 or AnyCpu is often larger depending on the type of code/information included in the dll. AnyCpu is probably the largest, although I haven't built many libraries, so may be off base.

  23. #23

    Goshx is offline

    Thread Starter

    Fanatic Fellow member


    Re: Drawing inside od picturebox

    Hmmmm.... dunno
    BB's demo was compiled for AnyCpy )))

    Well, Ive tried BB's code from post #10 and it works nice.

    A cute, effective and simple piece of code.

    Mayhap I'chiliad asking as well much (no doubt) but I'd similar to come across how would look like drawing of rectangles or circles. It is non so easy as drawing of dots under mouse arrow. I remembered that I was drawing rectangles on picturebox many years ago but information technology was difficult to visually evidence how new shape would await like after releasing of mouse button.


  24. #24

    Re: Cartoon inside od picturebox

    I don't have time to look at the code once more right now, simply in general, information technology isn't a difficult affair to practise.
    I usually simply take advantage of having Autoredraw set to False, then I can draw a temporary rectangle that is easily erased by just using the cls function. In that location is also a rubberbanding type API function.
    Too, in boops boops demo that you've been referencing he is using a Base of operations image, and a zooming prototype, and allows transferring the zoomed image to the base of operations epitome, and then that same technique could be used as some other way to describe a figure over another prototype, then modify the base image when the mouse is released.

  25. #25

    Goshx is offline

    Thread Starter

    Fanatic Fellow member


    Re: Drawing inside od picturebox

    we'll await on boops boops to come. I remember that calculation of "disengage" of drawn shapes wouldn't be a bad idea. It shouldn't be difficult to implement.

Posting Permissions

  • You may not mail service new threads
  • Yous may not mail service replies
  • Y'all may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] lawmaking is On
  • HTML code is Off

Click Here to Expand Forum to Full Width

Source: https://www.vbforums.com/showthread.php?819011-Drawing-inside-od-picturebox

Posted by: burtonegary1949.blogspot.com

0 Response to "How To A Drawing Picturebox Vb.net"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel