Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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.
Posted 14-Feb-13 22:01pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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");
  Permalink  
v3
Comments
vebi1000 at 15-Feb-13 4:14am
   
Yes its ok but how to save picturebox image which exist in usercontrol and i am trying to save this image from mainform.
vebi1000 at 15-Feb-13 4:17am
   
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.
OriginalGriff at 15-Feb-13 4:39am
   
Answer updated
vebi1000 at 15-Feb-13 5:23am
   
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();
}
OriginalGriff at 15-Feb-13 5:35am
   
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100