views:

1269

answers:

3

I have below function to generate a sample logo. What I want to do is to return a transparent png or gif instead of white background. How can I do it?

private Bitmap CreateLogo(string subdomain)
{

    Bitmap objBmpImage = new Bitmap(1, 1);
    int intWidth = 0;
    int intHeight = 0;
    Font objFont = new Font("Arial", 13, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
    Graphics objGraphics = Graphics.FromImage(objBmpImage);
    intWidth = (int)objGraphics.MeasureString(subdomain, objFont).Width;
    intHeight = (int)objGraphics.MeasureString(subdomain, objFont).Height;
    objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
    objGraphics = Graphics.FromImage(objBmpImage);
    objGraphics.Clear(Color.White);
    objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    objGraphics.DrawString(subdomain, objFont, new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
    objGraphics.Flush();
    return (objBmpImage);


}

here is the end result

context.Response.ContentType = "image/png";
            using (MemoryStream memStream = new MemoryStream()) 
            { 
                CreateLogo(_subdname).Save(memStream, ImageFormat.Png); 
                memStream.WriteTo(context.Response.OutputStream); 
            }

function

private Bitmap CreateLogo(string subdomain)
{

    Bitmap objBmpImage = new Bitmap(1, 1);
    int intWidth = 0;
    int intHeight = 0;
    Font objFont = new Font("Arial", 13, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);
    Graphics objGraphics = Graphics.FromImage(objBmpImage);
    intWidth = (int)objGraphics.MeasureString(subdomain, objFont).Width;
    intHeight = (int)objGraphics.MeasureString(subdomain, objFont).Height;
    objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
    objGraphics = Graphics.FromImage(objBmpImage);
    objGraphics.Clear(Color.Transparent);
    objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
    objGraphics.DrawString(subdomain, objFont, new SolidBrush(Color.FromArgb(255, 255, 255)), 0, 0);
    objGraphics.Flush();
    return (objBmpImage);

}
A: 

You can do something like this:

        Bitmap bmp = new Bitmap(300, 300);
        Graphics g = Graphics.FromImage(bmp);

        g.Clear(Color.Transparent);
        g.FillRectangle(Brushes.Red, 100, 100, 100, 100);

        g.Flush();
        bmp.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);
Tim Croydon
+1  A: 

Disclaimer: I'm pimping for my company, Atalasoft.

We sell an imaging toolkit that includes codecs for GIF and for PNG that support transparency. GIF is a little trickier because it is a colormapped image format and Graphics doesn't really like drawing on colormapped images. PNG has full alpha support and is easier to work with.

You can write code with our library to get transparent png images like this:

// image in your size, width
AtalaImage image = new AtalaImage(imageWidth, imageHeight,
        PixelFormat.Pixel32bppBgra, Color.FromArgb(0, 255, 255, 255));
Graphics g = image.GetGraphics();

// perform drawing - whatever

PngEncoder encoder = new PngEncoder();
encoder.Save(outputStream, image, null);
image.Dispose();

To work with GIF, you'd do something similar except that you fill the initial image with a color you won't use for anything else, then at the point above where I make the PngEncoder, you convert the image to colormapped:

AtalaImage eightBit = image.GetChangedPixelFormat(PixelFormat.Pixel8bppIndexed);
byte index = eightBit.Palette.GetClosestPaletteIndex(myBackgroundColor);
GifEncoder encoder = new GifEncoder();
encoder.BackgroundIndex = index;
encoder.Save(outputStream, image, null);
eightBit.Dispose();
image.Dispose();
plinth