1

I'm working on a project where I need the ability to load images into the program and drag them around, which means that they will have to be able to overlap. The program needs to be able to work with different image formats, such as jpg and png.

I started out using pictureboxes, which worked fine until I loaded a png with an alpha channel and for some, to me at least,Visual Studio's seems to handle transparency in a very odd way.

Picture box 1

In this picture, the black and white flags are a png image, which from what I understand appears to have a white background, as it uses its parent backcolor, which in this case is Form1.

So what I can do is change its parent to the picture on the right and it will do this.

Picture 3 child of Picture 2

But this is where the issue occurs, as the flags are cut off by picturebox 2.

So as an alternative I used Graphics, drawimage to draw them on Form1 which allows me to drag them around the screen as well, by hooking them up to some small pictureboxes.

Using Drawimage

Now the flags can overlap all images using the correct alpha.

The issue with this is that when I drag them around I have to redraw them at the new position and use Me.Refresh() to clear the screen, but this causes them to flicker a lot.

Which is obviously really annoying. What can I do to fix it? Or am I approaching this all wrong, to begin with?

I'm quite new to VB so I have browsed the internet for answers, trying stuff that people have suggested, but from what I can see they are not really trying to achieve what I need, as they draw their stuff in fixed controls like panels etc. or they are trying to make drawing programs, where you have a canvas. But I need to be able to drag images around freely and place them on top of each other.

  1. I have added these lines to Form1, which did nothing from what I can see:

    Me.SetStyle(ControlStyles.UserPaint, True)
    Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
    Me.DoubleBuffered = True
    
  2. I have tried to see if I could just refresh the region around the graphics being moved but again doesn't seem to really do anything.

  3. I have tried moving the drawimage to the form1 paint event.

  4. I have tried using panels, pictureboxes, labels and any other control that would allow me to add an image :D

  5. Tried looking into the translate, but that leaves "ghosts" behind, which forces me to use the Me.refresh()

Mobster
  • 11
  • 3
  • What happens when you don't use Me.Refresh? – Niya May 01 '23 at 21:47
  • It will draw the images all over the screen where you drag them if that makes sense? Meaning it will draw the image every time the "isDragging" is called. I have to admit that when I started the program, it didn't even cross my mind that transparent controls would turn out to be my biggest issue, I thought it was a given that it would work as it does in all other programs :) I am considering looking into using DirectX and that it might be able to solve it, but I haven't gotten around to testing that yet. – Mobster May 02 '23 at 19:57

0 Answers0