Locked How to set transparent image for bitmap

  • יום שלישי 01 מאי 2012 14:28
     
     

    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,

כל התגובות

  • יום שלישי 01 מאי 2012 15:02
     
     

    If the bitmap support transparency, then try this:

        . . .

        objBmpImage.MakeTransparent( Color.Transparent );

        objGraphics = Graphics.FromImage( objBmpImage );

        objGraphics.Clear( Color.Transparent );      

        . . .

  • יום שלישי 01 מאי 2012 15:13
     
     

    hie,

    i tried but its not working.

  • יום שלישי 01 מאי 2012 23:23
     
     

    ...

        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

  • יום רביעי 02 מאי 2012 05:25
     
     

    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.

    • נערך על-ידי krutik יום רביעי 02 מאי 2012 06:19
    •  
  • יום רביעי 02 מאי 2012 08:12
     
     

    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.
  • יום רביעי 02 מאי 2012 08:45
     
     

    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.

  • יום רביעי 02 מאי 2012 09:43
     
     

    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

  • יום רביעי 02 מאי 2012 09:50
     
     תשובה קוד כלול

    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

  • יום רביעי 02 מאי 2012 10:22
     
     תשובה

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



  • יום רביעי 02 מאי 2012 10:49
     
     

    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.
  • יום רביעי 02 מאי 2012 10:51
     
     

    hi,

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

    • הוצע כתשובה על-ידי Rajasekhar.R יום רביעי 02 מאי 2012 11:46
    • הצעה כתשובה בוטלה על-ידי Rajasekhar.R יום רביעי 02 מאי 2012 11:46
    •