Safwan
Safwan

Reputation: 171

how to achieve alpha icon transparency of mapped application

I have a timesheet application which maps the current application on timebased,i am using the mapped application icon as symbol of indication,i am unable to achieve the transparency,as black background is occuring along with the application icon when mapping is done,here is my code,i have tried to achieve it through many codes but unable to find any solution,i think there is a problem in the converter class of image

here is the xaml code for it

 <Image Source="{Binding Path=IconBinding, Converter={StaticResource imageConverter} }" Canvas.Left="100" Canvas.Top="10" Height="35"/>

and this is the code for imageconverter class

 public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        // empty images are empty...
        if (value == null) { return null; }

        var image = (System.Drawing.Image)value;
        // Winforms Image we want to get the WPF Image from...
        var bitmap = new System.Windows.Media.Imaging.BitmapImage();
        bitmap.BeginInit();
        MemoryStream memoryStream = new MemoryStream();
        // Save to a memory stream...
        image.Save(memoryStream, ImageFormat.Bmp);
        // Rewind the stream...
        memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
        bitmap.StreamSource = memoryStream;
        bitmap.EndInit();
        return bitmap;
    }

    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return null;
    }
}

Upvotes: 2

Views: 174

Answers (1)

Safwan
Safwan

Reputation: 171

I found the solution ,actually bitmap doesnt supports transparency,it supports but you have to hard code a bit,i found an easy solution to it ,you have to make the image in png format ,than u can achieve transparency.,so just changed a single line

image.Save(memoryStream, ImageFormat.Bmp);

change this line in converter class to

 image.Save(memoryStream, ImageFormat.Png);

Upvotes: 1

Related Questions