tags:

views:

644

answers:

5

Hello all, Im trying to generate a small square 32 by 32 pixels with a 10 by 10 squareish transparent gap in the middle.

This is what I have so far:

private Image CreatePicture(){
    // Create a new Bitmap object, 32 x 32 pixels in size
    Bitmap canvas = new Bitmap(32,32,System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
    for(int i=10;i<21;i++){
     for(int p=10;p<21;p++){
      canvas.SetPixel(i,p,Color.Lime);
     }
    }
    canvas.MakeTransparent(Color.Lime);
    // return the picture
    return canvas;
}

Its a it rough and not going to be the final "optimized version" its just a rough demo script. The problem is the returned image does not transparency instead its just a grey box :(.

Any help appreciated.

Michael

UPDATE: I have updated the script with the PixelFormat set to an Alpha RGB format which it actually accepts without erroring on runtime. Now though if I remove the "canvas.MakeTransparent(Color.Lime);" line it shows a lime box in the middle with it it just shows a grey box the same colour as the grey background; so it seems as if transparency is being recognised just not implimenting the transparency!

private Bitmap CreatePicture(){
    // Create a new Bitmap object, 50 x 50 pixels in size
    Bitmap canvas = new Bitmap(82,82,System.Drawing.Imaging.PixelFormat.Format32bppArgb);

    for(int i=10;i<71;i++){
     for(int p=10;p<71;p++){
      canvas.SetPixel(i,p,Color.Lime);
     }
    }
    canvas.MakeTransparent(Color.Lime);
    // return the picture
    return canvas;
}
+3  A: 
[...]Format16bppRgb555

Try to use some format with an alpha channel (...Rgba) here. Also, if the output will be an image later, make sure you use an image format like PNG that supports alpha channels.

schnaader
+1  A: 

You probably need to change the image format to Format16bppArgb1555.

Right now, you're using Format32bppArgb or Format16bppRgb555, which is 16 bits per pixel with no alpha information. Transparency requires an alpha channel or alpha bit to be present.

Reed Copsey
i tried this and i got:"Unhandled Exception: System.NotImplementedException: The requested feature is not implemented [GDI+ status: NotImplemented] at System.Drawing.GDIPlus.CheckStatus (Status status) [0x00000] at System.Drawing.Bitmap..ctor (Int32 width, Int32 height, .... " :(
redorkulated
Hrm... YOu may need to go to Format32bppArgb. This should work (it'll take more space), but as mentioned in another post, you'll want to save it as a PNG to preserve the alpha channel.
Reed Copsey
+1  A: 

It depends on how your going to use or display the image. PNG and Gif are the two file formats that support transparency.

For the bitmap setting of transparency, i used:

Graphics.FromIamge(bitmap).FillRectangle(Brushes.Transparent, ...) to set the area I wanted as transparent, then I saved the file out as a PNG to get an image with transparency.

I also tried the MakeTransparent method and ran into problems and the Brushes.Transparent was the solution that worked for my situation.

Hope this gives you a direction to look into.

One last note, I create the Bitmap object with width and height only, I don't specify the pixel format or whatever that's called i.e. bitmap = New Bitmap(width, height);

Bryan Bailliache
thanks for the help but none of them seem to make any diffrence just the same grey box
redorkulated
ok still getting a grey box on the screen but when i call "canvas.Save("Test.png",System.Drawing.Imaging.ImageFormat.Png);" before returning it the image saved has full transparency! I now see the problem must be something else :(.
redorkulated
A: 

My don't you just SetPixel() to Color.Transparent? Instead of Color.Lime then MakeTransparent? And as others have noted; you need a pixel format with an alpha channel.

Phil Price
A: 

The MakeTransparent function does work. You have "grey patches" because your TransparencyKey is not correct. You'll find that if you set your TransparencyKey property to whatever color that grey patch is, the grey patches will indeed become transparent when your image is displayed. The default grey patch I got was labeled as "Control" in color, or basically the default Windows Form background color. Change your TransparencyKey to whatever your grey patch is (in my case Control) and your problem will be solved, no saving of files to the Hard Drive neccessary. Cheers mate.