How do I pick a colour on PictureBox and make it transparent
-
I would like to be able to pick a colour on a PictureBox control and make it transparent. How can I then save the image as a gif file with transparency. I have a program that draws shapes on an image in a picturebox, however when I save the image none of the shapes that I have drawn are saved. What do I need to do to achieve this. Any help is appreciated. Jim
-
I would like to be able to pick a colour on a PictureBox control and make it transparent. How can I then save the image as a gif file with transparency. I have a program that draws shapes on an image in a picturebox, however when I save the image none of the shapes that I have drawn are saved. What do I need to do to achieve this. Any help is appreciated. Jim
OK, first of all, are you drawing the shapes on the PictureBox's Graphics, or a Graphics created from the bitmap? You need to create a graphics from the bitmap using Graphics.FromImage(), and then draw on that. About transparency, I really don't know how to do that with GIFs in GDI+. I believe the first entry in the palette is the transparent color, but I don't know how to set that with GDI+.
**"Have a heart that never hardens, a temper that never tires, a touch that never hurts." -- Charles Dickens
-
OK, first of all, are you drawing the shapes on the PictureBox's Graphics, or a Graphics created from the bitmap? You need to create a graphics from the bitmap using Graphics.FromImage(), and then draw on that. About transparency, I really don't know how to do that with GIFs in GDI+. I believe the first entry in the palette is the transparent color, but I don't know how to set that with GDI+.
**"Have a heart that never hardens, a temper that never tires, a touch that never hurts." -- Charles Dickens
Thanks for your reply :) In my program the PictureBox is either loaded from a file: pictureBox.Image = Image.FromFile(dlg.FileName) Or from a stream: Dim binaryData(-1) As [Byte] binaryData = Base64Utility.GetByteArray(_ImageData) Dim ms As New MemoryStream(binaryData) _PictureBox.Image = Image.FromStream(ms) To draw on the PictureBox I am calling the Invalidate method which then fires the Paint event: pictureBox.Invalidate() In the paint event I am passing the PaintEventArgs to this Draw Sub. Public Sub Draw(ByVal p As Pen, ByVal e As PaintEventArgs) Implements IHotspot.Draw If _bEndIsSet Then e.Graphics.DrawRectangle(p, New Rectangle(_ptOrigin.X, _ptOrigin.Y, (_ptEnd.X - _ptOrigin.X), (_ptEnd.Y - _ptOrigin.Y))) e.Graphics.DrawRectangle(p, _ptEnd.X - 2, _ptEnd.Y - 2, 4, 4) 'e.Graphics.DrawString(CStr(_ptOrigin.X) & ":" & CStr(_ptOrigin.Y), New Font("arial", 8, FontStyle.Regular), Brushes.Yellow, _ptOrigin.X, _ptOrigin.Y) End If End Sub So instead of using e.Graphics I should be using Graphics.FromImage() to draw on? Jim
-
Thanks for your reply :) In my program the PictureBox is either loaded from a file: pictureBox.Image = Image.FromFile(dlg.FileName) Or from a stream: Dim binaryData(-1) As [Byte] binaryData = Base64Utility.GetByteArray(_ImageData) Dim ms As New MemoryStream(binaryData) _PictureBox.Image = Image.FromStream(ms) To draw on the PictureBox I am calling the Invalidate method which then fires the Paint event: pictureBox.Invalidate() In the paint event I am passing the PaintEventArgs to this Draw Sub. Public Sub Draw(ByVal p As Pen, ByVal e As PaintEventArgs) Implements IHotspot.Draw If _bEndIsSet Then e.Graphics.DrawRectangle(p, New Rectangle(_ptOrigin.X, _ptOrigin.Y, (_ptEnd.X - _ptOrigin.X), (_ptEnd.Y - _ptOrigin.Y))) e.Graphics.DrawRectangle(p, _ptEnd.X - 2, _ptEnd.Y - 2, 4, 4) 'e.Graphics.DrawString(CStr(_ptOrigin.X) & ":" & CStr(_ptOrigin.Y), New Font("arial", 8, FontStyle.Regular), Brushes.Yellow, _ptOrigin.X, _ptOrigin.Y) End If End Sub So instead of using e.Graphics I should be using Graphics.FromImage() to draw on? Jim
Jim Taylor wrote: So instead of using e.Graphics I should be using Graphics.FromImage() to draw on? Correct. In VB6, you could draw on the picturebox and what you drew would show up on the image, but with VB.NET, this doesn't happen. IMO it's much better that way.
**"Love does not delight in evil but rejoices with the truth." -- 1 Corinthians 13:6
-
I would like to be able to pick a colour on a PictureBox control and make it transparent. How can I then save the image as a gif file with transparency. I have a program that draws shapes on an image in a picturebox, however when I save the image none of the shapes that I have drawn are saved. What do I need to do to achieve this. Any help is appreciated. Jim
If anyone else needs to solve this problem take a look at: Using a Color Matrix to Transform a Single Color and Creating Transparent GIF Images Jim