如何在c#中保存图片框图像 [英] how to save picturebox image in c#

查看:142
本文介绍了如何在c#中保存图片框图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是编程c#的新手,运行Windows窗体应用程序。我有一个mainform和一些其他用户控件。我在其中一个用户控件中有一个图片框。我想保存图片框的图像。我在该使用控件中有一个名为save的按钮,因此我可以使用以下行保存。

 picBottle.Image.Save(Application.StartupPath +   \\ +   Volumetransfer +(GetNextNodeID())。ToString()+   .BMP); 





现在我已将该保存按钮移至mainfrom,因此当我尝试使用相同的代码时,我无法使用。当我点击主窗体中的保存按钮时如何保存该图像。



请帮助我。在此先感谢。

解决方案

首先,不要保存到那个位置 - 它可能会产生问题,因为它不是用于数据(这是一个坏的练习将其用于此类)。要么专门为他们创建一个文件夹,要么看看这里:我应该在哪里存储我的数据? [ ^ ]

指定文件格式也是一个想法:

 picBottle.Image.Save(  @  D:\Temp\xx.bmp,System.Drawing.Imaging.ImageFormat。 Bmp); 

对我来说很好。



你应该找到解决问题的方法!



vebi1000 - 19分钟前

是的还可以,但如何保存usercontrol中存在的picturebox图像,我正在尝试从mainform保存此图像。

回复

vebi1000 - 16分钟前

还有一件事就是当我点击主要的保存按钮时,从当前时间开始显示在用户控件中的文本框的e。




如果是这种情况,那么我将在userc ontrol中创建一个保存图像的公共方法到指定的文件名,并在文本框中设置时间。然后主窗体可以在需要保存时调用该方法。

这样,用户控件负责它的内容 - 而不是主要形式 - 这是一个更简洁的设计OOP的观点。并且更容易实现...

任何其他方法都意味着将用户控件的内部暴露给主窗体,并且首先创建用户控件的原因之一是防止那个!





你能帮我解决一下如何从用户控制到mainform调用方法的简单示例代码片段。我在用户控制中创建了一个方法,但我很难调用这个方法。

  public   void  SaveImage()
{
picBottle.Image.Save( @ C:\ Users \Veera \Desktop,System.Drawing.Imaging.ImageFormat.Bmp);
txtTransferStartTime.Text = DateTime.Now.ToString();
}







我会通过路径作为参数-let形式担心关于在哪里存储它。这样,表单可以包含用户控件的多个实例,并且它们不会互相覆盖:

  public   void  SaveImage( string  path)
{
picBottle.Image。保存(path,System.Drawing.Imaging.ImageFormat.Bmp);
txtTransferStartTime.Text = DateTime.Now.ToString();
}

所有你要做的就是调用实例上的方法,就像你对任何其他控件一样。

所以如果调用你的控件类 MyUserControl 并且您已在设计器中向主窗体添加了一个实例,它可能被称为 myUserControl1 ,所以调用方法:

 myUserControl1.SaveImage( @  C:\Users\Veera\Desktop\myImage.bmp); 


Hi,

I am very new to programming c#, I running windows form application. I have one mainform and some other user controls. I have a picture box in one of the user control. I want to save that image of picturebox. I have one button called "save" in that use control so i am able to save using below line.

picBottle.Image.Save(Application.StartupPath + "\\"+"Volumetransfer"+(GetNextNodeID()).ToString()+".bmp");



Now i have moved that save button to mainfrom so when i tryy the same code i am not able. How to save that image when i click "save" button in the mainform.

Please help me. Thanks in advance.

解决方案

Firstly, don''t save to that location - it will probably give problems because it it not intended for data (and it is a bad practice to use it for such). Either create a folder specially for them, or have a look here: Where should I store my data?[^]
It''s also an idea to specify the file format:

picBottle.Image.Save(@"D:\Temp\xx.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

works fine for me.

You should find that cures your problem!

"vebi1000 - 19 mins ago
Yes its ok but how to save picturebox image which exist in usercontrol and i am trying to save this image from mainform.
Reply
vebi1000 - 16 mins ago
One more thing is at the same time when i click that "save" button in main from then current time has to display in one of the textbox in the usercontrol."


If that is the case, then I would create a public method in the userc ontrol that saved the image to a specified file name, and set the time in the text box. The main form can then call the method when it want''s it saved.
That way the user control is responsible for it''s content - not the main form - which is a cleaner design from the OOPs point of view. And easier to implement as well...
Any other approach would mean exposing the internals of the user control to the main form, and one of the reasons for creating user controls in the first place is to prevent that!


Can you help me with simple example code snippets that how to call a method from user control to mainform. I have created a method in user control but i am confusing to call this method.

public void SaveImage() 
        {
             picBottle.Image.Save(@"C:\Users\Veera\Desktop", System.Drawing.Imaging.ImageFormat.Bmp);
            txtTransferStartTime.Text = DateTime.Now.ToString();
        }




I would pass the path though as a parameter -let the form worry about where to store it. That way the form can contain multiple instances of the user control and they don''t overwrite each other:

public void SaveImage(string path)
    {
    picBottle.Image.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
    txtTransferStartTime.Text = DateTime.Now.ToString();
    }

All you have to do is call the method on the instance, just as you do with any other control.
So if your control class is called MyUserControl and you have added an instance to your main form in the designer, it is probably called "myUserControl1", so just call the method:

myUserControl1.SaveImage(@"C:\Users\Veera\Desktop\myImage.bmp");


这篇关于如何在c#中保存图片框图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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