0

I was drawing everything (image and all strings) but when I draw the image, that loads my processor and blinks the image, two things that I don't want...

To fix this problem, I tried this:

  1. Set the main image as background image then draw over it, but when you try to "Clear(color.transparent)", it turns all black and you've to draw it again
  2. Add another PictureBox over main PictureBox but when you do this, the front image hide everything (as shown in picture 2)

Note: The background image is static and will never change, the only thing that'll change is the drawstring's...

Static background image

Drawstring's

Overlap problem

  • 1
    If you just need to show the Image, set the template Bitmap as the Image of a PictureBox and draw the strings on the surface of the PictureBox. When you save the drawing, if necessary, draw the strings on a copy of the template and save to disc/print the copy. – Jimi Oct 25 '19 at 21:03
  • 1
    Showing your drawing code would be really helpful. – LarsTech Oct 25 '19 at 22:11
  • 1
    You should probably do as @Jimi suggested but, if you don't, you should be redrawing the background every time as well. If you ever need to change something, just be sure to call `Invalidate` and specify the area that has or may have changed. Your drawing code is very fast so don't worry about drawing a lot. It's the actual repainting that is slow so use `Invalidate` to ensure that as little area is repainted as possible. – jmcilhinney Oct 26 '19 at 01:09
  • You shouldn't have to call `Clear(Color.Transparent)`. What happens if you remove that call in your first scenario? – Visual Vincent Oct 26 '19 at 07:37
  • @Jimi but that's the problem, the string are constantly changing, so I've to redraw the image everytime. – Heitor Badotti Oct 28 '19 at 13:47
  • @jmcilhinney I tried to take a look in this Invalidate function, but when I try to call "e.Invalidate" it doesn't show up any function... Repainting only a specify area will fix my problem, but I don't know how to do it !!! – Heitor Badotti Oct 28 '19 at 13:49
  • @VisualVincent It paints 2 times in the same area as shown in the 3rd picture – Heitor Badotti Oct 28 '19 at 13:49
  • `Invalidate` is a method of the form, not of any event args. [Here](http://www.vbforums.com/showthread.php?879985-Why-does-the-Form_Paint-event-run-continuously-but-only-draw-once&p=5426439&viewfull=1#post5426439) is an example I wrote for someone else recently. – jmcilhinney Oct 28 '19 at 14:18
  • @jmcilhinney That's it! It solved my problem, invalidate only redraw strings, not needed to redraw image, that's much better... Just another question, I followed your example and if I "Invalidate(rectangle2)", why it refresh all image not only what's inside the rectangle? – Heitor Badotti Oct 28 '19 at 18:05
  • 1
    It doesn't. That's the whole point. Go back and read my first comment again. – jmcilhinney Oct 28 '19 at 22:12

1 Answers1

0

Please consider the following code snippet which might help you to solve the problem.

Private ReadOnly backImage As Bitmap = 'The path of your background image.

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim srcRect As New Rectangle(0, 0, backImage.Width, backImage.Height)
    Dim desRect As Rectangle = PictureBox1.ClientRectangle

    Using bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.SmoothingMode = SmoothingMode.HighQuality
            G.Clear(Color.Transparent)
            G.DrawImage(backImage, desRect, srcRect, GraphicsUnit.Pixel)
            G.DrawString("0.00", New Font("Arial", 12, FontStyle.Regular), Brushes.Red, desRect.Right - 80, 35)
            'and the other overlay painting ....
        End Using

        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
        e.Graphics.DrawImageUnscaled(bmp, 0, 0)
    End Using
End Sub

I don't know how do you update your painting routine, nor whether the rest of your code has a possible effect on the painting. I think the mentioned code snippet is more than enough. Here is a quick demo that Invalidates (NOT Refresh) the painting every 100 milliseconds:

SOQ58565613A

Do you see any flickering? and here is the reading from my task manager while the demo is running:

58565613B

Almost nothing.

I suggest that:

  • If you invalidate the painting through the TextChanged and/or ValueChanged events of the input controls, make sure that your controls are not in an infinite redundancies loop. Each event calls another event again and again. If not then:

  • Try your application in another machine just to make sure that your main one has no hardware issues. Maybe it's time for a new setup :).

  • 1
    A [PictureBox is DoubleBuffered](https://referencesource.microsoft.com/#system.windows.forms/winforms/managed/system/winforms/PictureBox.cs) by default. – LarsTech Oct 26 '19 at 01:57
  • Yes, I changed from Panel to Picturebox because of double buffered... And I tried to use "e.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed", but no much difference... – Heitor Badotti Oct 28 '19 at 13:59