C#中的pictureBox.Image.Save()不起作用 [英] pictureBox.Image.Save() in c# doesn't work

查看:451
本文介绍了C#中的pictureBox.Image.Save()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白这个问题.因为这通常到现在仍有效. 该方法是单击后将图像保存在名为canvas的pictureBox上的反应. 我将图像加载到画布上,然后进行一些编辑.然后我要保存图像. 如果我在加载图像之前单击printScreenButton,则它可以工作,但是当我加载图像时,它会停止工作. 问题可能出在哪里?

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); 
      }


创建名为name.png的工作==文件
不起作用==未创建名为name.png的文件


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
        }`

一开始就进行初始化:

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

推荐答案

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

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

从不编写这样的代码,您无需指定文件的完整路径.这使得文件的实际位置在很大程度上取决于程序的当前工作目录. Environment.CurrentDirectory的值.可能会发生意外更改,使用在未将RestoreDirectory属性设置为true的情况下使用OpenFileDialog就是一个示例.

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.

如果没有异常,则可以确保文件已保存.确切地,保存是一个猜测.至少使用SaveFileDialog或Environment.GetFolderPath()来获取可预测的目录名称.另外,默认的工作目录在用户的计算机上不起作用,您无法写入c:\ program文件.

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.

这篇关于C#中的pictureBox.Image.Save()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆