C#中的pictureBox.Image.Save()不起作用

编程入门 行业动态 更新时间:2024-10-26 10:34:06
本文介绍了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()不起作用

本文发布于:2023-11-07 01:10:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   pictureBox   Image   Save

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!