pictureBox.Image.Save() in c# doesn't work

2019-08-12 19:39发布

I don't understand this issue. Because this usually worked till now. This method is reaction on click for saving image on pictureBox which is called canvas. I load the Image on canvas and then do some edits. Then I want save the image. If I click on the printScreenButton before loading image it works, but when I load the image it stops working. Where could be the problem?

private void printScreenButton_Click(object sender, EventArgs e)
      {
          canvas.Image.Save("name.png", System.Drawing.Imaging.ImageFormat.Png); 
      }

Edit:
Work == file called name.png is created
Doesn't work == file called name.png isn't created


Code for drawing an image == putting on picture box

` private void drawTransformedBitmap(Matrix transformationMatrix) 
        {
            Graphics g = Graphics.FromImage(canvasBitmapShow); //prepare graphics

            if (antialiasing)
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
            }
            else 
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            }

            g.Clear(Color.White); //clear canvas

            g.Transform.Reset(); //clear transformations
            g.Transform = transformationMatrix; //set transformations from transformationMatrix
            g.DrawImage(canvasBitmapTarget, 0, 0); //draw from Bitmap called canvasBitmapTarget

            canvas.Invalidate(); //refresh canvas
        }`

Initialization at the begining:

canvasBitmapShow = new Bitmap(canvas.Width, canvas.Height);
canvasBitmapSource = new Bitmap(canvas.Width, canvas.Height);
canvasBitmapTarget = new Bitmap(canvas.Width, canvas.Height);
canvasBitmapBackup = new Bitmap(canvas.Width, canvas.Height);

canvas.Image = canvasBitmapShow; //set the Image

2条回答
我想做一个坏孩纸
2楼-- · 2019-08-12 19:56

When you load an image, I suppose using an OpenFileDialog, you changed the CurrentDirectory, simply set RestoreDirectory to true to prevent this behavior, but beware, apparently it sometimes behave differently across OS according to answers here

Maybe the easiest way to get around the problem is by using the SpecialFolder to save your file.

查看更多
对你真心纯属浪费
3楼-- · 2019-08-12 20:10

canvas.Image.Save("name.png", System.Drawing.Imaging.ImageFormat.Png);

Never write code like this, you don't specify the full path of the file. Which makes the actual location of the file heavily dependent on the current working directory of your program. The value of Environment.CurrentDirectory. Which can change unexpectedly, using an OpenFileDialog without the RestoreDirectory property set to true would be an example.

If you get no exception then you can be sure that the file got saved. Exactly where it got saved is a guess. At least use either SaveFileDialog or Environment.GetFolderPath() to get a predictable directory name. Also, the default working directory won't work on your user's machine, you cannot write to c:\program files.

查看更多
登录 后发表回答