locked
How to set transparent image for bitmap

    Question

  • hi,

    I am converting text to image using below code but i am facing problem setting transparency of bitmap.When i am setting graphic object using this function : objGraphics.Clear(Color.Transparent); I am getting black background.

    code:

      private Bitmap CreateBitmapImage(string sImageText)

    {

       Font objFont = new Font("Cheri", 10, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);

       Graphics objGraphics = Graphics.FromImage(objBmpImage);

       intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;
       intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;

        objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
        objGraphics = Graphics.FromImage(objBmpImage);

       objGraphics.Clear(Color.Transparent);       
       objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
       objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
       objGraphics.DrawString(sImageText, objFont,Brushes.Red, 0, 0);
       objGraphics.Flush();   
      

    return (objBmpImage);

    }

    can any one help me out when i try to set it to transparent it gives me black color background.I am new to graphic development so any help from your side will be appreciated

    Thanks In Advance,

    Tuesday, May 01, 2012 2:28 PM

Answers

  • assuming you want to create a new (transparent) Bitmap with the Text:

            private void button1_Click(object sender, EventArgs e)
            {
                string text = "Hello";
                this.pictureBox1.Image = WriteTextToNewImage(text);
            }
    
            private Image WriteTextToNewImage(string text)
            {
                Bitmap bmpOut = null;
                using (Font f = new Font("Comic Sans Ms", 24))
                {
                    //measure string
                    Size sz = TextRenderer.MeasureText(text, f);
    
                    bmpOut = new Bitmap(sz.Width, sz.Height);
    
                    //write to new Bitmap
                    using (Graphics g = Graphics.FromImage(bmpOut))
                    {
                        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                        g.DrawString(text, f, Brushes.Blue, new Point(0, 0));
                    }
                }
    
                return bmpOut;
            }

    Regards,

      Thorsten

    Wednesday, May 02, 2012 9:50 AM
  • Here's a transparent label that does the same thing as a transparent text only bitmap:

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
      
    public partial class Form1 : Form
      {
        
    public Form1()
        {
          InitializeComponent();
        }
        
    private MovableTransparentLabel MovableTransparentLabel1;
        
    protected override void OnLoad(EventArgs e)
        {
          
    base.OnLoad(e);
          
    this.DoubleBuffered = true;
          MovableTransparentLabel1 = 
    new MovableTransparentLabel(this);
        }
      }
      
    internal class MovableTransparentLabel
      {
        
    internal Control Parent;
        
    internal string Text = "MovableTransparentLabel";
        
    internal Font Font;
        
    internal StringFormat Stringformat = StringFormat.GenericDefault;
        
    internal Rectangle Bounds = new Rectangle(0, 0, 200, 20);
        
    internal Brush Brush = new SolidBrush(Color.Black);
        
    public MovableTransparentLabel(Control Parent)
        {
          
    this.Parent = Parent;
          
    this.Font = Parent.Font;
          Parent.Paint += Parent_Paint;
          Parent.MouseDown += Parent_MouseDown;
          Parent.MouseMove += Parent_MouseMove;
        }
        
    public void Parent_Paint(object sender, PaintEventArgs e)
        {
          e.Graphics.DrawString(Text, Font, Brush, Bounds, Stringformat);
        }
        
    private Point MDLoc = new Point();
        
    public void Parent_MouseDown(object sender, MouseEventArgs e)
        {
          
    if (this.Bounds.Contains(e.Location))
          {
            
    if (e.Button == MouseButtons.Left)
            {
              MDLoc.X = 
    this.Bounds.X - e.Location.X;
              MDLoc.Y = 
    this.Bounds.Y - e.Location.Y;
            }
          }
        }
        
    public void Parent_MouseMove(object sender, MouseEventArgs e)
        {
          
    if (this.Bounds.Contains(e.Location))
          {
            
    if (e.Button == MouseButtons.Left)
            {
              Parent.Invalidate(
    this.Bounds);
              
    this.Bounds.X = e.Location.X + MDLoc.X;
              
    this.Bounds.Y = e.Location.Y + MDLoc.Y;
              
    this.Parent.Invalidate(this.Bounds);
            }
          }
        }
      }
    }



    Wednesday, May 02, 2012 10:22 AM

All replies

  • If the bitmap support transparency, then try this:

        . . .

        objBmpImage.MakeTransparent( Color.Transparent );

        objGraphics = Graphics.FromImage( objBmpImage );

        objGraphics.Clear( Color.Transparent );      

        . . .

    Tuesday, May 01, 2012 3:02 PM
  • hie,

    i tried but its not working.

    Tuesday, May 01, 2012 3:13 PM
  • ...

        objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
        objGraphics = Graphics.FromImage(objBmpImage);

       objGraphics.Clear(Color.Transparent);       
    ...

    Hi,

    why dont you just create a blank 32bp bitmap of the desired width and height instead of clearing the Background (which may result in a black BG when the PixelFormat does not specify an alpha channel)?

    objBmpImage = new Bitmap(intWidth, intHeight);

    Regards,

      Thorsten

    Tuesday, May 01, 2012 11:23 PM
  • hi,

    i also tried this but this doesnot work

    Bitmap objBmpImage = new Bitmap(1, 1,PixelFormat.Format32bppArgb);

    what i am trying to achieve is convert my text to image with background transparent.

    • Edited by krutik Wednesday, May 02, 2012 6:19 AM
    Wednesday, May 02, 2012 5:25 AM
  • hi,

    i also tried this but this doesnot work

    Bitmap objBmpImage = new Bitmap(1, 1,PixelFormat.Format32bppArgb);

    what i am trying to achieve is convert my text to image with background transparent.


    How do you know it doesn't work?  What do you expect it to do?  You should have an 1 pixel bitmap.
    Wednesday, May 02, 2012 8:12 AM
  • hi friend,

    I suppose you havent gone through code my function take string as agrument

    private Bitmap CreateBitmapImage(string sImageText)

    and  based on text  i create bitmap width and size as you can see here;

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

    after successful conversion i try to save it to my local drive and view image background and even i have one demo page on that i test it.so i hope now you can understand how it works either way thanks for all your post i found my solution.

    Wednesday, May 02, 2012 8:45 AM
  • hi friend,

    I suppose you havent gone through code my function take string as agrument

    ...

    Well that method is exactyl what I'm wondering about... You have a Bitmap objBmpImage, but from where? Then you create a Graphics-object for this Bitmap, and almost immediately you create a new Bitmap object and assigne that to objBmpImage...

    So what do you want to do? Write some text to an existing Bitmap? Or write some Text to a new Bitmap?

    Regards,

      Thorsten

    Wednesday, May 02, 2012 9:43 AM
  • assuming you want to create a new (transparent) Bitmap with the Text:

            private void button1_Click(object sender, EventArgs e)
            {
                string text = "Hello";
                this.pictureBox1.Image = WriteTextToNewImage(text);
            }
    
            private Image WriteTextToNewImage(string text)
            {
                Bitmap bmpOut = null;
                using (Font f = new Font("Comic Sans Ms", 24))
                {
                    //measure string
                    Size sz = TextRenderer.MeasureText(text, f);
    
                    bmpOut = new Bitmap(sz.Width, sz.Height);
    
                    //write to new Bitmap
                    using (Graphics g = Graphics.FromImage(bmpOut))
                    {
                        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                        g.DrawString(text, f, Brushes.Blue, new Point(0, 0));
                    }
                }
    
                return bmpOut;
            }

    Regards,

      Thorsten

    Wednesday, May 02, 2012 9:50 AM
  • Here's a transparent label that does the same thing as a transparent text only bitmap:

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
      
    public partial class Form1 : Form
      {
        
    public Form1()
        {
          InitializeComponent();
        }
        
    private MovableTransparentLabel MovableTransparentLabel1;
        
    protected override void OnLoad(EventArgs e)
        {
          
    base.OnLoad(e);
          
    this.DoubleBuffered = true;
          MovableTransparentLabel1 = 
    new MovableTransparentLabel(this);
        }
      }
      
    internal class MovableTransparentLabel
      {
        
    internal Control Parent;
        
    internal string Text = "MovableTransparentLabel";
        
    internal Font Font;
        
    internal StringFormat Stringformat = StringFormat.GenericDefault;
        
    internal Rectangle Bounds = new Rectangle(0, 0, 200, 20);
        
    internal Brush Brush = new SolidBrush(Color.Black);
        
    public MovableTransparentLabel(Control Parent)
        {
          
    this.Parent = Parent;
          
    this.Font = Parent.Font;
          Parent.Paint += Parent_Paint;
          Parent.MouseDown += Parent_MouseDown;
          Parent.MouseMove += Parent_MouseMove;
        }
        
    public void Parent_Paint(object sender, PaintEventArgs e)
        {
          e.Graphics.DrawString(Text, Font, Brush, Bounds, Stringformat);
        }
        
    private Point MDLoc = new Point();
        
    public void Parent_MouseDown(object sender, MouseEventArgs e)
        {
          
    if (this.Bounds.Contains(e.Location))
          {
            
    if (e.Button == MouseButtons.Left)
            {
              MDLoc.X = 
    this.Bounds.X - e.Location.X;
              MDLoc.Y = 
    this.Bounds.Y - e.Location.Y;
            }
          }
        }
        
    public void Parent_MouseMove(object sender, MouseEventArgs e)
        {
          
    if (this.Bounds.Contains(e.Location))
          {
            
    if (e.Button == MouseButtons.Left)
            {
              Parent.Invalidate(
    this.Bounds);
              
    this.Bounds.X = e.Location.X + MDLoc.X;
              
    this.Bounds.Y = e.Location.Y + MDLoc.Y;
              
    this.Parent.Invalidate(this.Bounds);
            }
          }
        }
      }
    }



    Wednesday, May 02, 2012 10:22 AM
  • hi friend,

    I suppose you havent gone through code my function take string as agrument

    ...

    Well that method is exactyl what I'm wondering about... You have a Bitmap objBmpImage, but from where? Then you create a Graphics-object for this Bitmap, and almost immediately you create a new Bitmap object and assigne that to objBmpImage...

    So what do you want to do? Write some text to an existing Bitmap? Or write some Text to a new Bitmap?

    Regards,

      Thorsten


    well what i am trying i have one string and i want to convert it to image which will contain that text along with transparent background.I hope this makes you clear what my exactly code is does.
    Wednesday, May 02, 2012 10:49 AM
  • hi,

    thanks to all microsoft contributor i myself found solution for it.Thank you all.

    • Proposed as answer by Rajasekhar.R Wednesday, May 02, 2012 11:46 AM
    • Unproposed as answer by Rajasekhar.R Wednesday, May 02, 2012 11:46 AM
    Wednesday, May 02, 2012 10:51 AM