Need Help: Saving an image without using picturebox.image.save()
I want to be able to save images from a web page after it is loaded into a WebBrowser object. I can loop through the images on the web page, put each image in a picturebox and then use picturebox.image.save to save it. The problem is when my application is minimized or behind other windows, the picturebox doesn't redraw even with using Application.DoEvents() so I get an object refrence null error. What I want to know is if there is a way to put those images into memory and then save them from there without the use of a picturebox.
theLizardPosted Jan 28, 2010, 4:20 PM
You don't need a picture box
Bitmap b = new Bitmap(path);
b.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
Apart from routine error checking and other must do's these two lines of code is all you need to take an image using a path to its location and saving it to another location changing the format along the way IF you want to.
b can be saved to whatever is supported by System.Drawing.Imaging.ImageFormat.
Harvesting!!!!!
theLizardPosted Jan 30, 2010, 8:52 PM
Good work...
Jacob RPosted Jan 30, 2010, 7:23 PM
public static bool SaveFileFromURL(string url, string destinationFileName, int timeoutInSeconds)
{
// Create a web request to the URL
HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create(url);
MyRequest.Timeout = timeoutInSeconds * 1000;
try
{
// Get the web response
HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();
// Make sure the response is valid
if (HttpStatusCode.OK == MyResponse.StatusCode)
{
// Open the response stream
using (Stream MyResponseStream = MyResponse.GetResponseStream())
{
// Open the destination file
using (FileStream MyFileStream = new FileStream(destinationFileName, FileMode.OpenOrCreate, FileAccess.Write))
{
// Create a 4K buffer to chunk the file
byte[] MyBuffer = new byte[4096];
int BytesRead;
// Read the chunk of the web response into the buffer
while (0 < (BytesRead = MyResponseStream.Read(MyBuffer, 0, MyBuffer.Length)))
{
// Write the chunk from the buffer to the file
MyFileStream.Write(MyBuffer, 0, BytesRead);
}
}
}
}
}
catch (Exception err)
{
throw new Exception("Error saving file from URL:" + err.Message, err);
}
return true;
}
theLizardPosted Jan 28, 2010, 5:56 PM
If it worked for a while then the only problem is the lengths so you will just have to accept that you will not be able to save images from locations where the path is greater than max file and dir length.
Jacob RPosted Jan 28, 2010, 5:16 PM
System.IO.PathTooLongException was unhandled
Message="The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."
I don't see a way around this!
Thomas SidwellPosted Jan 28, 2010, 3:45 PM
Have you considered using the picturebox1.paint(); meathod? you could set it up when your application regains focus the event will fire so that in essence it is reloading the image in memory.
not sure if this will help, hope so.