如何创建透明位图图像?当我设置属性 Graphics.clear(color.Transparent ) 时,它会给出黑色背景。)

发布于 2024-11-26 13:11:33 字数 2734 浏览 9 评论 0原文

我在名为 displayImage.aspx 的 aspx 页面中使用一个标记,并分配其 src 属性值,该值是另一个名为 getImageFromText.aspx 的 aspx 页面

** src="getImageFromText.aspx" .**

我编写了在 getImageFromText 中创建位图的代码。 aspx.cs 并将其保存在内存蒸汽中,但我无法使该图像透明。当我设置 Graphics.clear(color.Transparent) 时,它会给出黑色背景tats yi 必须设置 Graphics.clear(color.ANYCOLOR) 删除黑色背景。 请给我建议或任何可以为位图图像制作透明背景的代码。
代码如下

protected void Page_Load(object sender, EventArgs e) {

    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();




}

I am using a tag in a aspx page named displayImage.aspx and assigning its src attribute value which is other one aspx page named getImageFromText.aspx

** src="getImageFromText.aspx" .**

I wrote code for creating bitmap in getImageFromText.aspx.cs and saving it in memory steam but i am not able to make that image transparent .When i set Graphics.clear(color.Transparent) then it gives black background tats y i have to set
Graphics.clear(color.ANYCOLOR) to remove that black background.
Please give me advice or any code from that i can make transparent background for bitmap image .
The code is below

protected void Page_Load(object sender, EventArgs e)
{

    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();




}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月无声 2024-12-03 13:11:33

使用:

Bitmap.MakeTransparent,通过使用类似一个参数,一个您希望变得透明的颜色。

问候。

Use:

Bitmap.MakeTransparent, by using like a parameter a color you wish to become transparent.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文