1

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
Mat
  • 202,337
  • 40
  • 393
  • 406
user1097772
  • 3,499
  • 15
  • 59
  • 95
  • 5
    What error are you getting? What is the actual problem? "It doesnt work" doesn't tell us much. – Tejs Jun 12 '12 at 14:30
  • 1
    please provide some more code like the one you're putting an image into the picture-box. – Tomtom Jun 12 '12 at 14:31
  • Put a `try..catch(Exception ex)` block around the `Save` call and take a look at the resulting exception. – CodeCaster Jun 12 '12 at 14:37
  • are you sure that file is not created ? Maybe is just in the wrong folder, set a full path for filename – Antonio Bakula Jun 12 '12 at 14:38
  • @AntonioBakula yes I'm sure its the path where is the .exe file and when I try to save it before loading picture => the file is created, so I also try to delete it, and I try it again after loading the image on canvas => no file created, I also tryed to refresh the folder => stil no file. – user1097772 Jun 12 '12 at 14:41
  • @CodeCaster I'll try it, but shouldn't it give me some error when there is an eror without puttiong try ... catch? I see no error, but i'll try try... catch – user1097772 Jun 12 '12 at 14:43

2 Answers2

3

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • OMFG, I would jump from the my boots after mistakes like that. Thanks a lot. I really haven't any clue that open file dialog do that. The pictures are of course saved in folder from the original picture was loaded. I'll use SaveFileDialog like you advised. Problem solved. – user1097772 Jun 12 '12 at 15:09
1

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.

Community
  • 1
  • 1
Martheen
  • 5,198
  • 4
  • 32
  • 55