Need to draw a simple image
I need to create a function which accepts a single value of integer which specifies the width/height of the resulting image, which will only have a single black pixel in the upper left corner and the rest being transparent and saved as a gif file. Any help on this would be appreciated.
Re: Need to draw a simple image
Is this the kind of thing you are looking for?
Code:
Function GIFBitmap(ByVal x As Integer, ByVal filename As String) As Boolean
Dim result As Boolean
Try
Dim bmp As New Bitmap(x, x)
bmp.SetPixel(0, 0, Color.Black)
bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Gif)
result = True
Catch
result = False
End Try
Return result
End Function
This would work, since a new bitmap declared only with its width and height is transparent so you only need to set the one pixel.
EDIT: No, I was wrong. Apparently Bitmap.Save doesn't save the transparency to a GIF file, so the transparent part will show up as black. You can find an explanation of how to make a transparent GIF (in Cs and VB.Net) here: http://www.bobpowell.net/giftransparency.htm
I don't have time to look at that myself now, but perhaps you can use it. I'll leave my Function code above since it may still be useful.
cheers, BB
Re: Need to draw a simple image
Ya, I think I saw that bobpowell link before and thought, wow, it can't be that hard to make a transparent image, can it?? As it turns out, there is a maketransparent comment in .NET, but it doesn't seem to work...
Re: Need to draw a simple image
Quote:
Originally Posted by
dbassettt74
Ya, I think I saw that bobpowell link before and thought, wow, it can't be that hard to make a transparent image, can it?? As it turns out, there is a maketransparent comment in .NET, but it doesn't seem to work...
I agree, that example is overcomplicated and it is full of stuff that was needed with VB6 or VB2003 but is no longer necessary. It does show that it's possible to save GIFs with transparency, though, and I'm sure it will be possible to boil it down to a dozen or so lines of code. I'll try to find time to look at it in the next few days, assuming no one comes up with something first.
MakeTransparent works on bitmaps, and you can do all kinds of things with transparency in GDI+. The problem is saving an image to a GIF, because the Save methods don't update the GIF palette with transparent colours. (Are you sure you need to use GIF files, by the way?;) )
cheers, BB
Re: Need to draw a simple image
I'm not sure if I "need" a gif file, but what I need is to produce some sort of transparent image with a single black dot in the upper left corner, to eventually be displayed by a web browser. So the web browsers has to render it has a transparent background. I thought this was only possible with a "gif", but maybe I'm wrong??
Re: Need to draw a simple image
The function I gave you in #2 will certainly work if you change the ImageFormat to Png or Bmp. They both save transparency data in the form of an Alpha byte for each pixel. Whether that data gets interpreted as "transparency" depends on the program reading it. GDI+ and many graphic programs treat it as you would expect. I'm sorry but I know next to nothing about web browsers, so perhaps someone else can advise you on that.
cheers, BB
Re: Need to draw a simple image
Png images will display fine in a web browser, Dbassette.