Results 1 to 20 of 20

Thread: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    45

    After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Me.Width = (Screen.PrimaryScreen.Bounds.Width)
    Me.Height = (Screen.PrimaryScreen.Bounds.Height)
    imgDelta.Image = System.Drawing.Image.FromFile("amit.jpg")
    Me.Visible = True
    Me.CreateGraphics.DrawImage(imgDelta.Image, 0, 0, Me.Width, Me.Height)
    Me.WindowState = FormWindowState.Maximized


    after performing Me.windowstate step in vb.net, form become blank and no jpeg mage is showing in form, and also when control goes from form_load method all draw image part gets blank.

    before Me.windowstate step ,form is painted with jpeg image.


    Kindly help me toresolve this issue.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,830

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    You drew on the form, then maximized it, causing it to refresh itself. This cause the Paint event to fire off which then erased the image you just drew.
    1) Set the Maximize first, then draw the image
    2) Draw the image in the Paint event and use the Graphics object that's passed into the event (in other words, don't go creating a new one)

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    45

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Quote Originally Posted by techgnome View Post
    You drew on the form, then maximized it, causing it to refresh itself. This cause the Paint event to fire off which then erased the image you just drew.
    1) Set the Maximize first, then draw the image
    2) Draw the image in the Paint event and use the Graphics object that's passed into the event (in other words, don't go creating a new one)

    -tg
    If we draw in paint event then t gets fire automatically in any time. This cause problem for me

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    45

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    can i do like this ,

    Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    If imgDelta.Image Is Nothing Then
    Me.CreateGraphics.DrawImage(imgDelta.Image, 0, 0, Me.Width, Me.Height)
    End If


    Tats what you r saying ?

    or i need to modify something kindly suggest me.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    45

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Hi techgnoe,

    and form become blank also when form_load method ends. What i need to do to retain "form image" which is drawn by draw image .

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,432

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Change Me.CreateGraphics to e.Graphics

    Quote Originally Posted by AmitMckvie View Post
    If we draw in paint event then t gets fire automatically in any time. This cause problem for me
    What problem(s) does it cause?

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Using CreateGraphics() is always wrong.

    If you draw outside the Paint event, everything you do will be erased when the next Paint event happens. So using CreateGraphics() means you're responsible for redrawing after every Paint event, which means you may as well draw in the Paint event.

    If we draw in paint event then t gets fire automatically in any time. This cause problem for me
    This doesn't make any sense to me. What kind of problem? I bet it's easier to solve that problem then the one in the original question.

    (haha me and si_the_geek had the same thought!)
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,830

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    what is it exactly you're trying to accomplish here? Why take the image of a picture box and draw it onto the form? are you trying to use it as the form's background?

    And yes, if you draw it in the paint event, it gets called automaticvally anytime something on the form changes. In many cases, when drawing on the form directly, that's what you want. Otherwise, if you drag another window over, it you'll lose any drawing on the form where the other form passed over it... that's why the paint event exists, so you can re-paint what ever you need.

    And what I had in mind was more along the lines of:
    Code:
    Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Me.CreateGraphics.DrawImage(imgDelta.Image, 0, 0, Me.Width, Me.Height)
    End Sub
    But yeah, depending on your needs, something like that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Quote Originally Posted by techgnome View Post
    [...]

    And what I had in mind was more along the lines of:
    Code:
    Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Me.CreateGraphics.DrawImage(imgDelta.Image, 0, 0, Me.Width, Me.Height)
    End Sub
    But yeah, depending on your needs, something like that.

    -tg
    You should switch that to e.Graphics. There's no reason to call CreateGraphics() there, and it creates a Graphics object you don't dispose so it could end up chewing through a lot of memory until the GC runs.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    45

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    I understood what you guys are suggesting me but, i am migrating vb 6.0 to vb.net . in vb 6.0 through paint picture they are painting a form and there are no paint event as well but same thing i wan to do in vb.net through draw image , it want paint event, in paintpicture after window maximize also forms retaining pictures. Thats why i confuse.

  11. #11
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,285

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Quote Originally Posted by Sitten Spynne View Post
    Using CreateGraphics() is always wrong.
    huh? what?
    ok, it is not the best choice here, but please: no rigid rules that Claim to be true in every circumstand.

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    45

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    In paint event i can not write paint method , my project something first i draw big 1 image with company logo under form_load method then through timer.tick method we draw 2,3,4... on top of 1 image.but once control reach in timer.tick event 1 gets dis appear but 2,3,4... is painted as normal.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,432

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    In VB6 forms automatically have a secondary image, and that is what you draw/write to. In the paint event, it copies that image to the form.

    You can do the same in VB.Net if you want, but you need to create the secondary image yourself.

    Quote Originally Posted by AmitMckvie View Post
    In paint event i can not write paint method , my project something first i draw big 1 image with company logo under form_load method then through timer.tick method we draw 2,3,4... on top of 1 image.but once control reach in timer.tick event 1 gets dis appear but 2,3,4... is painted as normal.
    It would have been better if you had shown the timer code, but in simple terms you should move the painting code from the timer to the paint event (use variables to determine what to paint if you need to), or you could use the secondary image approach.

  14. #14
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    It sounds like you're sort of trying to make a constantly animated background? Or do you just want it to animate the one time? Either way is doable, it just takes a little thought.

    If you have 4 images you're stacking on top of each other, you can represent the "state" of that with an Integer. 0 = just the background. 1 = background + image1, and so on. Each timer tick should change the state and Invalidate the Form. The Paint event should check the state and draw only the images the state indicates. To accomplish either effect:
    • For constant animation, the timer should change the state from 3 to 0 at the end.
    • For a one-time animation, the timer should stop at the end, and you could consider caching the image.


    Here's a quick demo of both behaviors:
    Code:
    Public Class Form1
    
        Private _animationTimer As Timer
    
        Private _loopAnimationState As Integer
        Private _constantAnimationState As Integer
    
        Private _images(3) As Image
    
        Public Sub New()
            Me.DoubleBuffered = True
            InitializeComponent()
            InitializeImages()
        End Sub
    
        Protected Overrides Sub OnShown(e As EventArgs)
            MyBase.OnShown(e)
    
            _animationTimer = New Timer()
            _animationTimer.Interval = 3000
            AddHandler _animationTimer.Tick, AddressOf WhenAnimationTimerTicks
    
            StartAnimations()
        End Sub
    
        Private Sub InitializeImages()
            _images(0) = CreateImage(Color.Red, 100)
            _images(1) = CreateImage(Color.Green, 75)
            _images(2) = CreateImage(Color.Yellow, 50)
            _images(3) = CreateImage(Color.Black, 25)
        End Sub
    
        Private Function CreateImage(ByVal color As Color, ByVal squareSize As Integer) As Image
            Dim bmp As New Bitmap(squareSize, squareSize)
            Using g As Graphics = Graphics.FromImage(bmp)
                g.Clear(color)
            End Using
    
            Return bmp
        End Function
    
        Private Sub StartAnimations()
            _loopAnimationState = 0
            _constantAnimationState = 0
    
            _animationTimer.Start()
        End Sub
    
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            MyBase.OnPaint(e)
    
            ' Make sure we don't try to draw before the images are initialized.
            If _images(0) IsNot Nothing Then
                DrawAnimatedRegion(e.Graphics, _loopAnimationState, New Point(25, 25))
                DrawAnimatedRegion(e.Graphics, _constantAnimationState, New Point(225, 25))
            End If
        End Sub
    
        Private Sub DrawAnimatedRegion(ByVal g As Graphics, ByVal state As Integer, ByVal baseLocation As Point)
    
            For stateIndex As Integer = 0 To state
                Dim stateImage As Image = _images(stateIndex)
                Dim imageLocation As New Point(baseLocation.X + (12 * stateIndex), baseLocation.Y + 12 * stateIndex)
                g.DrawImage(stateImage, imageLocation)
            Next
        End Sub
    
        Private Sub WhenAnimationTimerTicks(ByVal sender As Object, ByVal e As EventArgs)
            If _constantAnimationState < 3 Then
                _constantAnimationState += 1
            End If
    
            If _loopAnimationState <> 3 Then
                _loopAnimationState += 1
            Else
                _loopAnimationState = 0
            End If
    
            Invalidate()
        End Sub
    
    End Class
    Quote Originally Posted by digitalShaman View Post
    huh? what?
    ok, it is not the best choice here, but please: no rigid rules that Claim to be true in every circumstand.
    I wrote WinForms controls professionally for 6 years, and I've been answering questions about it for 14 years. I have yet to find a situation where I needed to call CreateGraphics(). It always creates more work because you have to coordinate with what goes on in the Paint event, and the easiest way to coordinate with the Paint event is "just handle the Paint event".
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  15. #15
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,000

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Well, technically if you wanted to simulate the persistent AutoRedraw buffer of a VB6 application in .Net, you can create your own bitmap as si_the_geek mentioned and assign it to the BackGroundImage of your form.
    You would then draw into that bitmap and call the form's invalidate method so that it repaints itself from that background image, i.e. the background image of the form is a reference to the same object that you created so you can draw into it using your reference and the form will refresh itself from its reference to the same object.

    In that case you don't have to have any code in the paint event since the form will automatically refresh itself from the background image when needed or directed.

    I modified your code from post #1 below. I'm not sure if you really need imgDelta, but assuming it is a picturebox, I left that line of code in and used it as is.

    I added a button to show that you can update the bitmap using your reference and see the update when the form redraws itself.
    Code:
    Public Class Form1
      Dim myBackGroundImage As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
      Dim rand As New Random
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Width = (Screen.PrimaryScreen.Bounds.Width)
        Me.Height = (Screen.PrimaryScreen.Bounds.Height)
        imgDelta.Image = System.Drawing.Image.FromFile("c:\c\tst.jpg")
        Using g As Graphics = Graphics.FromImage(myBackGroundImage)
          g.DrawImage(imgDelta.Image, 0, 0, Me.Width, Me.Height)
        End Using
        Me.WindowState = FormWindowState.Maximized
        Me.BackgroundImage = myBackGroundImage
        DoubleBuffered = True
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Using g As Graphics = Graphics.FromImage(myBackGroundImage)
    
          g.FillEllipse(Brushes.Red, rand.Next(1000), rand.Next(1000), 20 + rand.Next(100), 20 + rand.Next(100))
        End Using
        Invalidate()  'so the form redraws itself and you see your updated bitmap.
      End Sub
    End Class
    Last edited by passel; Jul 13th, 2017 at 11:48 AM.

  16. #16

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    45

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Hi Passel,

    I am migrating code from vb 6.0 to vb.net .

    I have one doubt that i need to draw one image in form_load() method and after ending form_load method control is going to timer_tick event and there we paint 2,3,4,5...6 on top of 1(main image which is drawn in form_load() method).

    in vb 6.0 they use form.paintpitcure( imgdelta.image,0,0,,me.width,me.height) in form_load method and after drawing it ,control goes to timer_tick method but painted part is not disappeared.

    But same thing i did through me.creategraphics.drawimage(imgdelta.image,0,0,me.width,me.height) in form_load method and as soon as control coming out from form_load method painted part gets disappered in timer_tick event .

    kindly suggest me.

  17. #17
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,000

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Well it depends on what you're doing in the timer event to paint the numbers on top.
    And of course there are many ways to accomplish the same appearance.
    If the numbers should appear on the background image, but then be erased and replaced by another number then you wouldn't want to draw the number to the bitmap, but draw the number in the paint event along the lines suggested by Sitten Spynne.

    When you call the Invalidate method, the form will refresh the background automatically from the background image assigned, thus clearing anything that was drawn in a previous paint event. Therefore, you wouldn't have to specifically erase the number because it would disappear automatically since it is not part of the background image. Then you can draw anything you want "on top" of that background image in the Paint event.

    I modified my previous example. You can still click on the button to draw a red ellipse to the bitmap at any time and it becomes a permanent part of the bitmap. But now a timer is added that will increment a number, and call the invalidate method.
    The invalidation will cause a paint event and in the paint event the number is printed at a location of the form. Whatever is drawn in the paint event is essentially dynamic. If you want to "erase" the number permanently, you just have a flag that skips drawing the number in the paint event, and trigger the paint event to "erase" it, i.e. you won't draw it so it won't be there.
    Code:
    Public Class Form1
      Dim myBackGroundImage As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
      Dim rand As New Random
      Dim frmCount As Long
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Width = (Screen.PrimaryScreen.Bounds.Width)
        Me.Height = (Screen.PrimaryScreen.Bounds.Height)
        imgDelta.Image = System.Drawing.Image.FromFile("c:\c\tst.jpg")
        Using g As Graphics = Graphics.FromImage(myBackGroundImage)
          g.DrawImage(imgDelta.Image, 0, 0, Me.Width, Me.Height)
        End Using
        Me.WindowState = FormWindowState.Maximized
        Me.BackgroundImage = myBackGroundImage
        DoubleBuffered = True
        Timer1.Interval = 500
        Timer1.Start()
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Using g As Graphics = Graphics.FromImage(myBackGroundImage)
    
          g.FillEllipse(Brushes.Red, rand.Next(1000), rand.Next(1000), 20 + rand.Next(100), 20 + rand.Next(100))
        End Using
        Invalidate()
      End Sub
    
      Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        frmCount += 1
        Invalidate()
      End Sub
    
      Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Using f As Font = New Font("Courier", 96, FontStyle.Bold)
          e.Graphics.DrawString(frmCount.ToString, f, Brushes.Blue, 400, 400)
    
        End Using
      End Sub
    End Class
    There are many other ways end up with the same visual effect, i.e. doing all the drawing in the paint event, both background and numbers, and many ways to accomplish that as well, like using multiple bitmaps to draw into, and then draw them collectively in the paint event. This only scratches the surface of options available.

  18. #18
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,285

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Quote Originally Posted by Sitten Spynne View Post
    I wrote WinForms controls professionally for 6 years, and I've been answering questions about it for 14 years. I have yet to find a situation where I needed to call CreateGraphics(). It always creates more work because you have to coordinate with what goes on in the Paint event, and the easiest way to coordinate with the Paint event is "just handle the Paint event".
    sure if you stay just with control drawing. but how do you draw into a newly created bitmap without using .CreateGraphics()? so that the bitmap can be saved later on or drawn in paint event or whatever.

  19. #19
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    Quote Originally Posted by digitalShaman View Post
    sure if you stay just with control drawing. but how do you draw into a newly created bitmap without using .CreateGraphics()? so that the bitmap can be saved later on or drawn in paint event or whatever.
    I did it in the example above. You create the Bitmap you want, then use Graphics.FromImage().

    Control.CreateGraphics() is a method of Control. You can't use it to draw on a bitmap. It's used to obtain a Graphics object capable of drawing on a Control. That drawing will be erased on the next invalidation cycle, so you also have to go make sure you redraw whatever it is in the Paint event. So it turns out it's all-around easier to handle the Paint event and call Invalidate() to raise Paint instead of having your drawing code duplicated in two places. It's also a little more complex because you become responsible for calling Dispose() on that Graphics object at the appropriate time.

    Bitmaps/images don't have an invalidation cycle like controls, so the only way to draw to them is to call Graphics.FromImage(), their equivalent to Control.CreateGraphics(). Because there's no other way, you're correct to state this is the "right" way.

    But when I said "CreateGraphics()", I specifically meant Control.CreateGraphics(), not "...and the analog for any other type of graphical object".
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  20. #20
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,285

    Re: After doing Me.windwstate=formwindowstate.maxmized then form becomes blank.

    i do apologize! i mixed up .CreateGraphics and Graphics.FromImage(). I had a bmp.CreateGraphics method in mind that in fact does not exist :P

    i'm just allergic against rigid rules but thats prop. my wicked personality

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width