update image

B

brenny

I'm saving image to any folder(no write,read permisions) by using
image1.save method.

I have accomplished this but I want to update this image,I want to change
this image with another image or same image and I use save method but an
error is occurred as "a generic error occurred in gdi+"

How can I accomplish this process?

note : image is taken as

String path;
Stream myStream;

OpenFileDialog openFileDialogPicture = new OpenFileDialog();

openFileDialogPicture.InitialDirectory = "C:\\images\\" ;

openFileDialogPicture.Filter = "picture files(*.jpg)|*.jpg";

openFileDialogPicture.FilterIndex = 2 ;

openFileDialogPicture.RestoreDirectory = true ;

if(openFileDialogPicture.ShowDialog() == DialogResult.OK)

{

if((myStream = openFileDialogPicture.OpenFile())!= null)

{

path = openFileDialogPicture.FileName;

image1 = Image.FromFile(path);

}

}
 
G

Guest

Well...!
You have two options for this.
1) When saving the image using the image1.save method make sure that you
delete the existing image and save the new image.
2)Try using a database to store the image path. When loading the image you
can do something like this
Dim img As Image
img = Image.FromFile(imagePath)
picPhoto.Image = img
picPhoto.Width = 126
picPhoto.Height = 95
hope this helps you
cheers
 
M

Morten Wennevik

Hi brenny,

The image locks the file when you open it directly from a file.
Read the file into a MemoryStream and use Image.FromStream

if((myStream = openFileDialogPicture.OpenFile())!= null)
{
// Read the file into a byte array, then close the file
byte[] b = new byte[myStream.Length];
myStream.Read(b, 0, b.Length);
myStream.Close();

// Create a MemoryStream holding the file data
MemoryStream ms = new MemoryStream(b);

// Load the image with Image.FromStream
image1 = Image.FromStream(ms);

// can be used when saving the file
path = openFileDialogPicture.FileName;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top