views:

434

answers:

2

Hi i have the following code... where i generate my own image and paste in to excel worksheet, please can you tell me why its background color is always BLUE? is it possible somehow to change that for example to make it transparent or white? (i tried flag.MakeTransparent(); but it also does not work)

       Bitmap flag = new Bitmap(200,200);
       using (Graphics g = Graphics.FromImage(flag))
        {
            g.DrawString("N", new Font("Verdana", 80, FontStyle.Bold), 
                         new SolidBrush(Color.Black), new PointF(40, 30));
        }
       System.Windows.Forms.Clipboard.SetDataObject(flag, true);
       ws.Paste(range, Type.Missing);

when i use the following

       flag.Save("C:\\image.png",System.Drawing.Imaging.ImageFormat.Png);

the image.png has transparent background...

thanks a lot!

A: 

Have you tried filling the image first thing?

 Image i = new Bitmap(200, 200);
 Graphics g = Graphics.FromImage(i);
 g.FillRectangle(Brushes.Transparent, 0, 0, 200, 200);
Rob Elliott
it still has the BLUE background...
A: 

I know this is answer to an old post, but in case anyone else finds it on google (like I did):

The bmp does not contain background information, therefore it has to be converted:

MemoryStream ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Png); IDataObject dataObject = new DataObject(); dataObject.SetData("PNG", false, ms); System.Windows.Forms.Clipboard.SetDataObject(dataObject, false);

The solution is copied from this address, where the full solution is: http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.dotnet.framework.drawing&tid=64a90f25-9dd5-4d27-8d69-02993d0a35ed&cat=en_US_fa599017-a9ec-4987-b602-c19ba8e5a4da&lang=en&cr=US&sloc=&p=1

Maja