2011-07-27 53 views
1

我在名为displayImage.aspx的aspx页面中使用了一个标记,并将其src属性值指定为其他如何创建透明位图图像?当我设置属性Graphics.clear(color.Transparent)时,它会产生黑色背景。)

我写的代码在getImageFromText.aspx.cs创建位图,保存到内存中的蒸汽,但我不能够命名getImageFromText.aspx

** SRC =“getImageFromText.aspx”一个aspx页面。**到使图像透明。当我设置Graphics.clear(color.Transparent)然后它给黑色背景tats yi必须设置 Graphics.clear(color.ANYCOLO R)删除黑色背景。 请给我建议或任何代码,我可以使位图图像的透明背景。
该代码是下面

保护无效的Page_Load(对象发件人,EventArgs的) {

if (Request.QueryString["phone"] != null) 
    { 
     CreateBitmapImage(Request.QueryString["phone"]); 
    } 

    // CreateBitmapImage("Call Now 123-457-1222"); 

} 
private void CreateBitmapImage(string phonenumber) 
{ 

    string message = "Call Now " + phonenumber.ToString(); 
    Bitmap objBmpImage = new Bitmap(1, 1); 



    int intWidth = 0; 

    int intHeight = 0; 


    // Create the Font object for the image text drawing. 
    FontFamily[] fontFamilies; 
    PrivateFontCollection fontCollection = new PrivateFontCollection(); 
    fontCollection.AddFontFile(Server.MapPath("Futura-Condensed-Bold.ttf")); 
    fontFamilies = fontCollection.Families; 
    string familyName = ""; 
    familyName = fontFamilies[0].Name; 
    Font objFont = new Font(familyName, 19, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel); 




    // Create a graphics object to measure the text's width and height. 

    Graphics objGraphics = Graphics.FromImage(objBmpImage); 



    // This is where the bitmap size is determined. 

    intWidth = (int)objGraphics.MeasureString(message, objFont).Width; 

    intHeight = (int)objGraphics.MeasureString(message, objFont).Height; 



    // Create the bmpImage again with the correct size for the text and font. 

    objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight)); 



    // Add the colors to the new bitmap. 

    objGraphics = Graphics.FromImage(objBmpImage); 



    // Set Background color "#5496CA" 
    string xCol = "#5496CA"; 
    Color clearClr = System.Drawing.ColorTranslator.FromHtml(xCol); 
    objGraphics.Clear(clearClr); 

    objGraphics.SmoothingMode = SmoothingMode.AntiAlias; 
    objGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality; 
    objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias; 

    objGraphics.DrawString(message, objFont, new SolidBrush(Color.White), 0, 0); 



    MemoryStream memoryStream = new MemoryStream(); 
    objBmpImage.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp); 

    // Write the MemoryStream to the Response stream, and set content type to image/gif. 
    memoryStream.WriteTo(Response.OutputStream); 
    Response.ContentType = "image/gif"; 
    Response.End(); 

    // Clean up. 
    memoryStream.Close(); 
    objGraphics.Flush(); 




} 

回答

3

使用:

Bitmap.MakeTransparent,通过使用像的参数要成为透明的颜色。

问候。

+0

我用objBmpImage.MakeTransparent();但它给了我相同的结果。 –

+0

你真的称之为最后一次通话吗? – Tigran

+0

我叫它只是B4 ... MemoryStream memoryStream = new MemoryStream(); objBmpImage.Save(memoryStream,System.Drawing.Imaging.ImageFormat.Bmp); –

相关问题