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

一曲冷凌霜 提交于 2019-12-12 23:37:36

问题


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

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10998779/picturebox-image-save-in-c-sharp-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!