I have a Image (Frameworkelement) on my GUI.
There is a image in there. Now I’m performing a doubleclick at this image and I want, that
the Image saves itself and is going to be opened, with the default imageviewer.
My Code:
void image_MouseDown(object sender, MouseButtonEventArgs e)
{
//Wayaround, cause there is no DoubleClick Event on Image
if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
{
SaveToPng(((Image)sender), "SavedPicture.png");
Process.Start("SavedPicture.png");
}
}
void SaveToPng(FrameworkElement visual, string fileName)
{
var encoder = new PngBitmapEncoder();
SaveUsingEncoder(visual, fileName, encoder);
}
void SaveUsingEncoder(FrameworkElement visual, string fileName, BitmapEncoder encoder)
{
RenderTargetBitmap bitmap = new RenderTargetBitmap(
(int)visual.ActualWidth,
(int)visual.ActualHeight,
96,
96,
PixelFormats.Pbgra32);
bitmap.Render(visual);
BitmapFrame frame = BitmapFrame.Create(bitmap);
encoder.Frames.Add(frame);
using (var stream = File.Create(fileName))
{
encoder.Save(stream);
}
}
Opening the picture works fine with Process.Start. The problem is the saving, well it saves the picture: SavedPicture.png but, Its just black, so theres no graphic.. Maybe someone could tell me, whats wrong in my code or knows a better way of saving a image in WPF.
Problem solved.
I used this:
File.WriteAllBytes()to save the image from binaryformat