Wczytuję do programu BitmapImage:
fullBitmapImage = new BitmapImage(new Uri(@"../Assets/ControlImages/Bar/Bar_full.png", UriKind.Relative));
Potem wywołuje tą metodę :
private unsafe void SetBarImageCorrect()
{
Bitmap toRetBitmap=new System.Drawing.Bitmap(fullBitmapImage.PixelWidth,fullBitmapImage.PixelHeight,PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(toRetBitmap);
g.Clear(System.Drawing.Color.Transparent);
// Bitmap halfBitmap0=BitmapImage2Bitmap()
// Bitmap halfBitmap = BitmapImage2Bitmap(emptyBitmapImage);
//halfBitmap=halfBitmap.Clone(new System.Drawing.Rectangle(0, 0, halfBitmap.Width/2, halfBitmap.Height), halfBitmap.PixelFormat);
g.DrawImage(BitmapImage2Bitmap(fullBitmapImage),0,0);
//g.DrawImage(halfBitmap,(int)emptyBitmapImage.PixelWidth/2,0);
//BarImg.Source = Bitmap2BitmapImage(toRetBitmap);
// Bitmap toSaveBitmap=new Bitmap((int)emptyBitmapImage.Width,(int)emptyBitmapImage.Height,g);
toRetBitmap.Save("test.png",ImageFormat.Png);
}
...
private Bitmap BitmapImage2Bitmap(BitmapImage bitmapImage)
{
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);
return new Bitmap(bitmap);
}
}
Nie wiem czemu po zapisie obrazu do pliku nie jest zachowany kanał alfa, zamiast przeźroczystości mam białe paski, a dodatkowo jak widać na poniższym obrazku kolory oryginalnego paska są inne.
Oryginał:
Po zapisaniu: