开发者

Transparent graphics surface in VB.NET?

开发者 https://www.devze.com 2023-01-01 23:49 出处:网络
I am making an application in VB.NET that allows a user to highlight some text in images of documents so that the highlights could be sav开发者_如何转开发ed for future reference, but the original imag

I am making an application in VB.NET that allows a user to highlight some text in images of documents so that the highlights could be sav开发者_如何转开发ed for future reference, but the original images would not be modified. I figured I can achieve it by creating a graphics out of an image file and allowing the user to draw on that graphics. The problem is - the graphics can not be created from indexed image files, which many of mine are. I am wondering: is it possible to create a transparent graphics surface above an image in a PictureBox control, so that the user could draw on that surface, but it would appear as if he is drawing directly on an image?

Thank you!


(following assumes WinForms)


Actually you can. To display image, use .BackgroundImage property of PictureBox, not .Image. Don't forget to set correct value for .BackgroundImageLayout to disable default tiling and keep it in sync with .SizeMode property, so that overlay image is properly aligned. After you set image, create an overlay image (assume your PictureBox is PictureBox1:

Dim image = PictureBox1.BackgroundImage
Dim overlay As New Bitmap(image.Width, image.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(overlay)
g.Clear(Color.Transparent)
'draw something just to show this is working:'
g.DrawEllipse(Pens.Aqua, New Rectangle(0, 0, image.Width, image.Height))
g.Dispose()
PictureBox1.Image = overlay

Now you can draw on the overlay image. Alternatively, you can copy your image (draw it to the overlay for example).


No.

You need to make draw on a copy of the image and set the PictureBox to display the modified copy.


Actually, SLaks might be right that you can't overlay transparent controls and have that work as expected.

I thought you could avoid this but ... instead make your own control. Keep track of your highlights and annotations either as a second bitmap or as the original highlight/annotation data (whichever is more appropriate for your purpose). On Paint, you'll draw the original image and then you'll draw your bitmap (or draw each component of it) onto your control's Graphics surface.

You can do some tweaks to make it more responsive within the GDI+ framework and also within your own code.

0

精彩评论

暂无评论...
验证码 换一张
取 消