Results 1 to 7 of 7

Thread: Need to draw a simple image

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    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.

    Visual Studio 2010

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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
    Last edited by boops boops; Nov 16th, 2009 at 08:22 PM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    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...

    Visual Studio 2010

  4. #4
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Need to draw a simple image

    Quote Originally Posted by dbassettt74 View Post
    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

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091

    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??

    Visual Studio 2010

  6. #6
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Need to draw a simple image

    Png images will display fine in a web browser, Dbassette.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width