10 Replies - 1419 Views - Last Post: 12 October 2012 - 08:31 AM Rate Topic: -----

#1 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

problems with graphics

Posted 10 October 2012 - 12:14 PM

Hi everyone! This is my problem, I have class called MovingText, the pourpose of that class is to move text left or right in the picture box (1px per 50ms). In WIN XP everything works fine , text is moving smooth, but in WIN 7 it's not (it's flickering). Interesting thing is ,when I moving mouse cursor , then it looks fine and text is moving smooth.

In the timer tick event I call MovingText.MoveLeft then when the position of the text is changed ,then I call "Draw" sub to draw text on the PICBOX.

    Private Sub Draw()
        bit = New Bitmap(PICBOX.Width, PICBOX.Height)
        Using graph As Graphics = Graphics.FromImage(bit)
            'to remove "shadows" from text use this property (you don't need to do this in WIN XP)
            graph.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit
            ' draw string
            graph.DrawString(pText, MyFont, FontColor, STR(1).X, y)
            graph.DrawString(pText, MyFont, FontColor, STR(2).X, y)
        End Using

        PICBOX.Image = bit
    End Sub


I have try to change graphic properties,but nothing happens.
graph.SmoothingMode
graph.CompositingMode
graph.CompositingQuality

Is This A Good Question/Topic? 0
  • +

Replies To: problems with graphics

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: problems with graphics

Posted 10 October 2012 - 01:01 PM

I think what you're seeing is a lack of refreshing going on - unless you cursor around which makes the OS cause a repaint/refresh.

Line 11 you're resetting the picturebox.image... but the picturebox might need a little push to be refreshed within the form.
Try adding PICBOX.refresh() (or the VB equivilent) between your existing lines 11/12
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1029
  • View blog
  • Posts: 4,007
  • Joined: 02-July 08

Re: problems with graphics

Posted 10 October 2012 - 01:42 PM

I use...
Me.SetStyle(ControlStyles.DoubleBuffer _
     Or ControlStyles.UserPaint _
     Or ControlStyles.AllPaintingInWmPaint, _
     True)
   Me.UpdateStyles()

...to take complete control of the painting - this tells the OS the it should not try to decide when to refresh, but rather wait til I call Refresh or Invalidate on the control in question.
Was This Post Helpful? 0
  • +
  • -

#4 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: problems with graphics

Posted 10 October 2012 - 01:44 PM

that was good idea, i have try with PICBOX.Refresh at several places including line 11-12 but still nothing happens.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: problems with graphics

Posted 10 October 2012 - 01:52 PM

Well, it was a thought. But VB is out of my area of expertise. In some cases it all behaves the same as C# just with different syntax. But at this point I'm out of depth for VB specific behavior and knowledge.

You say it flickers.... Hmmm... What if the routine is being called faster than the movement really takes place? So when you check the position to calculate the new position the GUI hasn't really updated. So the paint keep reoccurring at the same location; thus flicker.

Maybe stick a couple other labels on the form to display the x,y expectations. Or have them shown in console.writeline. Normally I'd say put in a breakpoint and check the values, but that sort of stop in execution might be just enough to give the program time to actually re-paint so you'd never see the same values repeating.
Was This Post Helpful? 0
  • +
  • -

#6 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: problems with graphics

Posted 10 October 2012 - 03:36 PM

_HAWK_ i have try with your code in form load, and form constructor but nothing is changed.

Another thing I have found out, if I display image in Form.BackgroundImage (instead of PICBOX.Image) there is no flickering.

Then I have try with custom control (empty form) but that doesn't work.
I don't have any idea anymore..btw thank you all for helping me.
Was This Post Helpful? 0
  • +
  • -

#7 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: problems with graphics

Posted 11 October 2012 - 03:05 PM

I need someone to make a test, if someone is willing to do this all you need is one picturebox and timer, here is the code:
note: when you don't move mouse cursor, you will see flickering, and when you move cursor around, the text will move smooth
Imports System.Drawing
Imports System.Drawing.Graphics
Public Class Form1
    Private pos As Integer
    Private ptext As String = "TEST PROJECT"
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        pos = PictureBox1.Width
        DoubleBuffered = True
        Timer1.Interval = 20
        Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        pos -= 1
        If pos = 0 Then pos = PictureBox1.Width
        Dim bit As Bitmap
        bit = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using graph As Graphics = Graphics.FromImage(bit)
            graph.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            graph.TextRenderingHint = Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit
            graph.DrawString(ptext, PictureBox1.Font, New SolidBrush(Color.Blue), New Point(pos, 0))
        End Using
        PictureBox1.Image = bit
    End Sub
End Class

I have try with SetStyle, refreshing, but nothing works to me, maybe it has to do with my graphics card, i don't know,because everything works fine on my laptop(win xp).
Was This Post Helpful? 0
  • +
  • -

#8 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: problems with graphics

Posted 11 October 2012 - 11:17 PM

I did the test, and I saw flickering all the time, no matter if I moved the cursor around. I also tried with SetStyles on Form.Load, and Refresh() at the end of Timer1.Tick handling method. No change. I'm interested in this one too, because I have no experience with graphics myself.
Was This Post Helpful? 1
  • +
  • -

#9 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: problems with graphics

Posted 12 October 2012 - 07:43 AM

View Postlucky3, on 11 October 2012 - 11:17 PM, said:

I did the test, and I saw flickering all the time, no matter if I moved the cursor around. I also tried with SetStyles on Form.Load, and Refresh() at the end of Timer1.Tick handling method. No change. I'm interested in this one too, because I have no experience with graphics myself.

Thank you, I can't believe there is no flickering on WIN XP, there must be solution for this in WIN7.
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1029
  • View blog
  • Posts: 4,007
  • Joined: 02-July 08

Re: problems with graphics

Posted 12 October 2012 - 07:58 AM

I am not clear by what you mean by flicker. The canvas nor the form flicker, but the text does not roll out maybe as smooth as you would like - is that it?

This is how I use graphics:

  Private pos As Integer
  Private ptext As String = "TEST PROJECT"
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    pos = PictureBox1.Width
    DoubleBuffered = True
    Timer1.Interval = 20
    Timer1.Enabled = True
  End Sub
  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    pos -= 1
    If pos = 0 Then pos = PictureBox1.Width
    PictureBox1.Invalidate()
  End Sub

  Private Sub PictureBox1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
    e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    e.Graphics.DrawString(ptext, New Font("Garamond", 20), New SolidBrush(Color.Blue), New Point(pos, 20))
  End Sub

Was This Post Helpful? 0
  • +
  • -

#11 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 138
  • View blog
  • Posts: 838
  • Joined: 21-December 11

Re: problems with graphics

Posted 12 October 2012 - 08:31 AM

View Post_HAWK_, on 12 October 2012 - 07:58 AM, said:

I am not clear by what you mean by flicker. The canvas nor the form flicker, but the text does not roll out maybe as smooth as you would like - is that it?

Yes,exactly! I tried your way, but same thing happens, and again when mouse is on the move everything goes smooth.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1