-2

I'm having a problem with SaveFIleDialog, i followed a few steps from stackoverflow. The problem is when i dont use SaveFileDialog but this:

 private void SaveImage(Canvas canvas, string fileName)
    {
        RenderTargetBitmap renderBitmap = new RenderTargetBitmap(4646, 3890, 1500d, 1500d, PixelFormats.Pbgra32);

        canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
        canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));

        renderBitmap.Render(canvas);



        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

        using (FileStream file = File.Create(fileName))
        {
            encoder.Save(file);
        }
    }

and call it in the save button event, it works. but when i use it like this:

 private void SaveImage(Canvas canvas, string fileName)
    {
        RenderTargetBitmap renderBitmap = new RenderTargetBitmap(4646, 3890, 1500d, 1500d, PixelFormats.Pbgra32);

        canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
        canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));

        renderBitmap.Render(canvas);

        SaveFileDialog s = new SaveFileDialog();
        s.FileName = "Pic";
        s.DefaultExt = ".png";
        s.Filter = "Picture files (.png)|*.png";

        Nullable<bool> result = s.ShowDialog();
        if (result == true)
        {
            string filename = s.FileName;

        }
    }

it doesn't save the file at all. Any advice? What am I doing wrong?

AlexDev
  • 85
  • 1
  • 9
  • You *don't* save as there is no `encoder.Save(file);`? - pass the dialog selected name to the file stream as you do in the first example. The dialog just provides you with a file name string selected by the user, it does not/can not write anything to disk. – Alex K. Jan 11 '17 at 16:05

1 Answers1

2

You still must save the bitmap to a file. The only difference is that you should use the FileName property of the SaveFileDialog as the constructor argument of the FileStream:

private void SaveImage(Canvas canvas, string fileName)
{
    SaveFileDialog s = new SaveFileDialog();
    s.FileName = "Pic";
    s.DefaultExt = ".png";
    s.Filter = "Picture files (.png)|*.png";

    Nullable<bool> result = s.ShowDialog();
    if (result == true)
    {
        RenderTargetBitmap renderBitmap = new RenderTargetBitmap(4646, 3890, 1500d, 1500d, PixelFormats.Pbgra32);

        canvas.Measure(new Size((int)canvas.Width, (int)canvas.Height));
        canvas.Arrange(new Rect(new Size((int)canvas.Width, (int)canvas.Height)));

        renderBitmap.Render(canvas);

        string filename = s.FileName;
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));

        using (FileStream file = File.Create(filename))
        {
            encoder.Save(file);
        }
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88