VS 2013 VB.NET: After window maximized, form showing first white then start displaying image.-VBForums
Results 1 to 29 of 29

Thread: VB.NET: After window maximized, form showing first white then start displaying image.

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    VB.NET: After window maximized, form showing first white then start displaying image.

    Hi All,

    I am facing some issue in vb.net form application. After painting a image in paint event, in form_load() event, i am changing windowstate to maximized for that it is showing full white then it goes to paint event and doing paint. For that it is taking time for some 1 second then it enter in paint event.

    I want form should not show full white as soon we invoke application, it paint directly.

    Thanks & Regards
    Amit Kumar Jha

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,584

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Obviously there is something wrong with your code. Obviously we can't know what's wrong with the code if we can't see the code.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Can you post the code?
    Is the form doublebuffered?
    How large is the image?

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Hi Mike ,

    ya doublebuffered is true and image is large. we painted in paint method.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Code:
    Friend class Form1
     Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
            Me.visible=True
            Me.TpMost=True
            Initscreen()
            If numScreens <= 1 Then
                Me.WindowState = FormWindowState.Maximized
            End If  
    EndSub
    
    Private Sub Initscreen()
    
            Dim ii As Short
            On Error Resume Next
            If startImage <> 0 Then
                imgDelta.Image = System.Drawing.Image.FromFile(imagePath & "CsaLogon_" & CStr(startImage) & "s" & fext)
                Initialize = False
                Me.Invalidate(New Region(New Rectangle(0, 0, Me.Width, Me.Height)))
                Me.Update()
    EndSub
    
     Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            On Error Resume Next
            Dim img As System.Drawing.Image
            img = System.Drawing.Image.FromFile(imagePath & "CsaLogon_" & CStr(startImage) & "s" & fext)
            e.Graphics.DrawImage(img, 0, 0, Me.Width, Me.Height)
     EndSub
    EndClass
    Last edited by Shaggy Hiker; Sep 25th, 2017 at 10:52 AM. Reason: Added CODE tags.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    I pasted code. Kindly look into it. After executing this, i always get first white screen and then image come in full screen. I want to stop that white thing. Bypainting in paint event, flickering issues gone, so i need to paint in paintevent only.

  7. #7
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    One question, if the image is to ocupy the all form, why dont set it as backgroundimage?

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Image is not fix, it changed dynamically, i have to paint through drawimage only in paint method.

  9. #9
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Can you post image size?

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Hi Mike,

    My image size is 1280*1024. Is there are any solution you have to remove whitepage which comes after executing application.

    Screen get full white for 1 sec then image showing in full screen mode.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,923

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    What happens if you comment out the "Me.visible=True" in the load event handler.

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,923

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Also, what happens if you comment out these three lines in your InitScreen call, in addition to commenting out the "Me.visible=True" line.
    Code:
    'Initialize = False
    'Me.Invalidate(New Region(New Rectangle(0, 0, Me.Width, Me.Height)))
    'Me.Update()

  13. #13
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    I think passel is right, i removed everithing and left just this:

    Code:
    Public Class Form2
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
        End Sub
    
        Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
            Dim img As Image
            img = Image.FromFile("H:\REVELADAS\Grand Place\DSC06626.png")
            e.Graphics.DrawImage(img, 0, 0, Width, Height)
    
        End Sub
    
    End Class
    I do get the back color of the form for a fraction of a second but it loads prety quikly.
    Image size 2816x4313 pixels, 9,74Mb from a Sony DSLr
    Computer runing W10, I5, 8Gb Ram, old graphics card Nvidia GF 630 4GB, Screen 40" HD.

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,584

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Quote Originally Posted by Mike Storm View Post
    I think passel is right, i removed everithing and left just this:

    Code:
    Public Class Form2
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
        End Sub
    
        Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    
            Dim img As Image
            img = Image.FromFile("H:\REVELADAS\Grand Place\DSC06626.png")
            e.Graphics.DrawImage(img, 0, 0, Width, Height)
    
        End Sub
    
    End Class
    I do get the back color of the form for a fraction of a second but it loads prety quikly.
    Image size 2816x4313 pixels, 9,74Mb from a Sony DSLr
    Computer runing W10, I5, 8Gb Ram, old graphics card Nvidia GF 630 4GB, Screen 40" HD.
    You definitely wouldn't call Image.FromFile in the Paint event handler. That code loads an Image object from a file every time the form is repainted, so that's a big inefficiency there. I'm guessing that that is just a proof of concept but I don't want the OP to think that that's a good idea.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Hi Mike/jmcilhinney sir,

    Thanks for reply, but still for 1 second, i am able to see back bolor , i want as soon as my application invoked my form should show painted image. Is it posssible to do this?

    Thanks & Regards
    Amit Kumar Jha

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,107

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    As Jmc noted, don't load the image from file in the paint... if you have to load the image from the file, do it before you do anything with the form... then it's in memory... then in the paint event, use that in memory object. I'd probably grab the image in the constructor, then simply hold onto the image so that it can be used & reused in the paint w/o getting it from disk each time. Given the number of times paint may need to fire off, getting it from disk seems like a bad idea.

    -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??? *

  17. #17
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    You right, that was for testing proposes, couse mi computer is not a new machine neither has any special hardware and it can load relativly large images files, and that includes SONY DSLR RAW format that can go up to 45Mb with no big delay, he hasn't told how large is the actualy file, or what is trying to do?
    i still dont know if he is just displaying images, if it is a slideshow, or if actualy editing them.

  18. #18
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,584

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Quote Originally Posted by AmitMckvie View Post
    but still for 1 second, i am able to see back bolor
    Then you're still doing it wrong. If you are using different code now, show us that. Are you seeing the pattern? If your code doesn't work and you'd like us to help you fix it, we need to see it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,107

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Dunno... but he is in fact loading from disk everytime the pain event happens... look at the original code...
    AAACK! TWICE!

    Code:
    Private Sub Initscreen()
    
    Dim ii As Short
    On Error Resume Next
    If startImage <> 0 Then
    imgDelta.Image = System.Drawing.Image.FromFile(imagePath & "CsaLogon_" & CStr(startImage) & "s" & fext) <-- Loads from disk here!
    Initialize = False
    Me.Invalidate(New Region(New Rectangle(0, 0, Me.Width, Me.Height))) <-- this causes the paint to fire off...
    Me.Update()
    EndSub
    
    Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    On Error Resume Next
    Dim img As System.Drawing.Image
    img = System.Drawing.Image.FromFile(imagePath & "CsaLogon_" & CStr(startImage) & "s" & fext) <-- where there is a SECOND load from disk!
    e.Graphics.DrawImage(img, 0, 0, Me.Width, Me.Height)
    EndSub
    EndClass
    My guess he's got other issues related to loading from disk, given that there's OENR in both of those routines tells me that the image is getting load once, never disposed, so when the second load happens, the file is locked, so rather than fix the code to only load properly once and dispose the image when done, he does OERN in the paint... but then somewhere calls InitScreen again, but the image/file is still locked, so that load causes an error, and another OERN is added.... sigh....

    -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??? *

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,830

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Quote Originally Posted by techgnome View Post
    everytime the pain event happens

    -tg
    Well, you sure got the name of the event right.

    The white screen is because drawing happens in two steps (unless double buffered). The first step clears the area, the second step paints to the area. Normally, this happens so fast that the cleared image never paints to the screen. If it paints to the screen even briefly, you get a flash. You are getting much more than a flash, if the empty screen is shown for a full second.

    The first thing I'd do is get rid of the On Error Resume Next. Even if the line was useful, it is still legacy VB6, and a particularly nasty construct. All that is doing is saying, "if there is an exception...just ignore it." The thing is, aside from what TG was saying, exceptions are SLOW. You REALLY want to know about them, not ignore them. If they are problems, then they are problems you really, really, want to fix, because exceptions are so terribly slow.
    My usual boring signature: Nothing

  21. #21

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Hi ,
    I did the same thing what you guys told but still for less than 1 second white came. Still my backcolr is black but showing it is white. So i created sample project in that if we compile through debug mode then it first show white background in breakpoint instead of black ,then after we do continue it become black. So how we can made black background in back. Because it might happen that some thing goes wrong and i am using paint event, so it display white background.

    Thanks & Regards
    Amit Kumar Jha

  22. #22

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.BackColor = Color.Black
    Me.Visible = True
    MsgBox("amit")
    End Sub
    End Class


    .....................................................................

    In message box it is showing white background and after press ok it showing black one.

    ----------------------------------------------------------------------------------------

    this is my designer code

    Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
    If disposing AndAlso components IsNot Nothing Then
    components.Dispose()
    End If
    Finally
    MyBase.Dispose(disposing)
    End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
    Me.SuspendLayout()
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(14.0!, 27.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.BackColor = System.Drawing.SystemColors.Desktop
    Me.ClientSize = New System.Drawing.Size(663, 544)
    Me.DoubleBuffered = True
    Me.Font = New System.Drawing.Font("Arial", 18.0!)
    Me.ForeColor = System.Drawing.Color.Red
    Me.Margin = New System.Windows.Forms.Padding(7, 6, 7, 6)
    Me.Name = "Form1"
    Me.ShowIcon = False
    Me.Text = "Form1"
    Me.TopMost = True
    Me.TransparencyKey = System.Drawing.Color.Red
    Me.ResumeLayout(False)

    End Sub

    End Class

  23. #23

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Kindly suggest me how to achieve this behavior in form .

  24. #24

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Name:  Capture.jpg
Views: 31
Size:  8.0 KB

  25. #25

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    It is coming while debugging made form1.visible true, is it possible to make full screen black in vb.net

  26. #26

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    fresh code form form1.vb
    Code:
    Public Class Form1
        Dim img As Image
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim configPath As String
            Dim imagePath As String
                imagePath = "path1"
    
            Me.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width
            Me.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height
            img = System.Drawing.Image.FromFile(imagePath & "image1.jpg")
            Me.WindowState = FormWindowState.Maximized
        End Sub
        Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            'On Error Resume Next
            e.Graphics.DrawImage(img, 0, 0, Me.Width, Me.Height)
        End Sub
    End Class
    Last edited by Shaggy Hiker; Sep 27th, 2017 at 10:20 AM. Reason: Added CODE tags.

  27. #27

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    44

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    for less than second also, i am able to see white blank screen after that my image came.

  28. #28
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,923

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    I tried your code and it seems to work fairly well for me. Perhaps you graphics card and/or driver are under powered.
    One thing you should do is set the Doubledbuffered attribute so that you don't see the form's client area in an undrawn state. It should draw first in memory, then show the results to the screen after the drawing is complete. I didn't see then need to set the form's size if you're going to maximize the form, but then again it may be in case the user selects to "restore down" the form. It shouldn't make any difference in the way the form initially appears in any case.
    Code:
    Public Class Form1
    
      Dim img As Image
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim configPath As String
        Dim imagePath As String
        imagePath = "c:\c\"
        DoubleBuffered = True
        img = System.Drawing.Image.FromFile(imagePath & "image1.jpg")
        Me.WindowState = FormWindowState.Maximized
      End Sub
    
      Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'On Error Resume Next
        e.Graphics.DrawImage(img, 0, 0, Me.Width, Me.Height)
      End Sub
    End Class

  29. #29
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    3,822

    Re: VB.NET: After window maximized, form showing first white then start displaying im

    Honestly the next step is difficult and a "good" solution is complex. I was trying to work on it, but for some reason on my machine 'draw this image at (0, 0)' was placing the image at (50, 50) and I ran out of time to deal with it.

    Here's the problem: your image is very, very big. If it's a JPEG or other complex format, it has to be decoded before it can be dealt with by GDI. In the end, you end up with roughly 10 megabytes of pixel data in memory at a resolution greater than 2000x1000.

    The first performance hurdle was loading the file from disk. 10MB isn't a lot of data, but hard drives perform in tens of milliseconds in a world where nanoseconds matter. That's a big deal. So people helped you load the file from disk only once. That was a big performance boost!

    The next performance hurdle is because you are rescaling the image every time you draw the form. You tell DrawImage() to use the form's width and height, but that's much smaller than the size of the image. So every time your Paint event happens, the computer has to spend a lot of time going over all 10 MB of pixel data and deciding which pixels to drop so it can draw a smaller version of the image. That can take a long time, particularly since Windows Forms is rarely hardware-accelerated.

    So the first attempt at a fix would be to very early on resize the image. Here's a bad attempt, building on passel's example from #28:
    Code:
    Public Class Form1
    
        Private _originalImage As Image
        Private _resizedImage As Image
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim configPath As String
            Dim imagePath As String
            imagePath = "c:\c\"
            DoubleBuffered = True
            _originalImage = System.Drawing.Image.FromFile(imagePath & "image1.jpg")
            Me.WindowState = FormWindowState.Maximized
        End Sub
    
        Private Sub form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            If _resizedImage Is Nothing
                Dim imageSize As New Size(Me.ClientRecangle.Width, Me.ClientRectangle.Height)
                _resizedImage = New Bitmap(_originalImage, imageSize)
            End If
    
            e.Graphics.DrawImage(_resizedImage, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height)
        End Sub
    End Class
    If the "resized" image doesn't exist, it's created via a convenience constructor on Bitmap. That constructor lets you resize an image by providing the original and the new desired size. I used ClientRectangle instead of the form's Width and Height directly. Width and Height include the borders and title bar of your form: that's too big! ClientRectangle represents the part of the form inside the borders, and is almost always what you want to use when drawing.

    Once the resized image exists, it's drawn. (For some reason, this was displaying at (50, 50) in my test project. But I was remoting into a Windows machine and using an 8K resolution BMP so there were any number of reasons I might have been seeing oddities. I didn't have time to keep investigating and I'm suspicious it was a problem with just my machine.)

    I say this is a "bad" approach because for best quality, if the form resizes you also have to resize the background image. Doing that efficiently is tricky, and the "best" approach would resize in "steps" and perhaps keep a cache of certain sizes. I could spend days refining that algorithm but it doesn't really help you get closer to finishing.

    Another quirk: the image HAS to be resized once, and I'm suspicious that takes a second or two. So at startup there'll be a slight delay. This could MAYBE be addressed by asynchronously resizing the image, but I'm not sure how well Image/Bitmap cooperate with that approach. The main problem is, "Resizing would best be done before the form is displayed, but we won't know the form's dimensions until it is displayed." Maybe the solution there is "use the screen's resolution as a guide. That's your decision.

    Also pay attention to what passel warns: sometimes slow machines just do things slowly. Resizing and rendering images takes time. Faster machines do it faster. If your machine is on the slower end and you're trying to work with very high resolution images, some things are just going to be slow. I can't tell you what speeds are sufficient for which images, you just have to experiment and see what works.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.