0

I would like some help with my images. I just want to save my screen and gladly I could. But some softwares can't properly see them. For example: If I put my image (.jpg) in facebook, or open it with Paint, it will be like this: Image looking fine!

but if I open the same image (the exactly same image) with the default Windows Program or some, it will appear like this:

Crappy Image

So, I was doing this "printscreen" in my WPF application, here is my code:

    private void btSalvar_Click_1(object sender, RoutedEventArgs e)
    {
        BitmapSource bmp = VisualToBitmap.Render(gridSalvar);
        Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
        dialog.Filter = "Imagem JPG(*.jpg)|*.jpg";
        if (dialog.ShowDialog() == true)
        {
            System.Windows.Media.Imaging.PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            System.IO.FileStream fs = new System.IO.FileStream(dialog.FileName, System.IO.FileMode.Create);
            encoder.Save(fs);
            fs.Close();
        }

And the class created to do so:

class VisualToBitmap
{
    public static System.Windows.Media.Imaging.BitmapSource Render(System.Windows.Media.Visual Visual, int Resolution = 96)
    {
        System.Windows.Media.Imaging.RenderTargetBitmap render;
        double width, height;
        int pixelWidth, pixelHeight;
        width = (double)Visual.GetValue(System.Windows.FrameworkElement.ActualWidthProperty);
        height = (double)Visual.GetValue(System.Windows.FrameworkElement.ActualHeightProperty);
        pixelWidth = Convert.ToInt32((width / 96) * Resolution);
        pixelHeight = Convert.ToInt32((height / 96) * Resolution);
        render = new System.Windows.Media.Imaging.RenderTargetBitmap(
            pixelWidth, pixelHeight, Resolution, Resolution,
            System.Windows.Media.PixelFormats.Pbgra32);
        render.Render(Visual);
        return render;
    }
}

can someone please help me fix this? Thanks guys!

Andre Aquiles
  • 394
  • 1
  • 17
  • Without a good [mcve] that reliably reproduces the problem, it's impossible to know for sure what you need to fix. But my first suggestion would be to make sure your root element in the XAML has a non-transparent background. E.g. set `Background="White"`. – Peter Duniho May 23 '16 at 01:25
  • See also http://stackoverflow.com/q/5464800 for additional possible hints. – Peter Duniho May 23 '16 at 01:28
  • OMG, I can't even believe it was just this. What a rookie mistake! Seriously, thanks for this. Guess I just should have to think outside the box. Really appreciated @PeterDuniho – Andre Aquiles May 23 '16 at 03:07
  • Possible duplicate of [How to set Transparency color for the output of RenderTargetBitmap?](http://stackoverflow.com/questions/5464800/how-to-set-transparency-color-for-the-output-of-rendertargetbitmap) – René Vogt May 23 '16 at 11:15

1 Answers1

0

Alright, so like Peter Duniho suggested, It was a rookie mistake. Just adding a background into an grid made the trick.

Andre Aquiles
  • 394
  • 1
  • 17