Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerate transparent PNG c#
    primarykey
    data
    text
    <p>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?</p> <pre><code>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); } </code></pre> <p>here is the end result</p> <pre><code>context.Response.ContentType = "image/png"; using (MemoryStream memStream = new MemoryStream()) { CreateLogo(_subdname).Save(memStream, ImageFormat.Png); memStream.WriteTo(context.Response.OutputStream); } </code></pre> <p>function </p> <pre><code>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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload