Question: I have few numbers which i wanted to show in tray icon... for that i need to convert those numbers into icon or any bitmap image ....need help is their any class or property in wpf c# to do so.. any method .. the method i tried is not working...

```
Font fnt = new Font("Tahoma", 8 );
fntheight += fnt.Height;
int maxheight = 0;

if (state == true)
    maxheight = height + fntheight + fntheight;
else
    maxheight = height + fntheight;

Graphics grf = Graphics.FromImage(myimg);
Image newimg = new System.Drawing.Bitmap(width, maxheight);
Graphics newgrf = Graphics.FromImage(newimg);

newgrf.FillRectangle(Brushes.White, 0, 0, width, height);

```

but this code in not working in current wpf c# .net 4+

Answer: 
First you need to add:

```
Using System.Drawing.Imaging;
using System.Windows.Interop;

```

To convert text to Bitmap:

```
public Bitmap TextToBitmap(string txt, string fontname, int fontsize, Color bgcolor, Color fcolor, int Width, int Height)
    {
        Bitmap bmp = new Bitmap(Width, Height);
        using (Graphics graphics = Graphics.FromImage(bmp))
        {
            Font font = new Font(fontname, fontsize);
            graphics.FillRectangle(new SolidBrush(bgcolor), 0, 0, bmp.Width, bmp.Height);
            graphics.DrawString(txt, font, new SolidBrush(fcolor), 0, 0);
            graphics.Flush();
            font.Dispose();
            graphics.Dispose();
        }
        return bmp;
    }

```

To convert Bitmap to BitmapImage:

```
public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
    {
        IntPtr hBitmap = bitmap.GetHbitmap();

        BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
            hBitmap,
            IntPtr.Zero, 
            Int32Rect.Empty, 
            BitmapSizeOptions.FromEmptyOptions());

        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        MemoryStream memoryStream = new MemoryStream();
        BitmapImage bitImage = new BitmapImage();

        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
        encoder.Save(memoryStream);

        bitImage.BeginInit();
        bitImage.StreamSource = new MemoryStream(memoryStream.ToArray());
        bitImage.EndInit();

        memoryStream.Close();

        return bitImage;
    }

```

To set the converted BitmapImage to an element that uses `System.Windows.Media.Brush` 

```
    public void SetBitmapImage()
    {
        System.Drawing.Bitmap bmp = TextToBitmap(*whatever goes here);

        *Element.Background = new ImageBrush(BitmapToBitmapImage(bmp));
    }

```

To set the converted BitmapImage to an element that uses `System.Windows.Media.Image` 

```
    public void SetBitmapImage()
    {
        System.Drawing.Bitmap bmp = TextToBitmap(*whatever goes here);

        *Element.Source = BitmapToBitmapImage(bmp);
    }

```
@MikMagic You can convert from System.Drawing.Bitmap to System.Windows.Media.Image via BitmapSource object.

See [this Code Project's article](http://www.codeproject.com/Articles/104929/Bitmap-to-BitmapSource) and [this MSDN document](http://msdn.microsoft.com/en-us/library/ms748873.aspx).