A parameter is not valid

D

David

Hi,

I am scanning an image and putting it into a picture box. (I am using WIA)

ImageFile img = (ImageFile)scanItem.Transfer(WIA.FormatID.wiaFormatJPEG);

string scanTime = DateTime.Now.ToFileTime().ToString();

string FileName = "c:\\tempimage_" + scanTime + ".jpg";

img.SaveFile(FileName);

pictureBox1.Image = Image.FromFile(FileName);

Refresh();

My picture box is inside a panel... the picturebox size mode is set to
AutoSize and the panel has autoscrolling. This is so that I can navigate
around the picture.

However, with the code above, I am now trying to grab hold of the picture to
upload it to a webservice... (The code below is AFTER putting the picture in
the picture box)


System.IO.BinaryReader br = new
System.IO.BinaryReader(System.IO.File.Open(FileName,
System.IO.FileMode.Open, System.IO.FileAccess.Read));

br.BaseStream.Position = 0;

byte[] buffer = br.ReadBytes(Convert.ToInt32(br.BaseStream.Length));

ScanService.ScanService uploadScan = new Scan.ScanService.ScanService();

uploadScan.PutScanFile(buffer);

br.Close();

File.Delete(FileName);


but the file was locked by the picturebox (or appears to be more
specifically, the Image.FromFile, so I changed the pictureBox loading to...

using (Image tempImage = Image.FromFile(FileName))

{

pictureBox1.Image = tempImage;

Refresh();

}

Now, that seemed to unlock the image and I could upload it and later delete
the file. However, if I attempt to scroll the panel with the picturebox
inside it, I get an error on the Application.Run line in my program.cs,
saying "A parameter is not valid".

So, without the above using statement, my pic is still scrollable, but not
uploadable or deleteable. With the using statement, my pic is uploadable and
deleteable but not scrollable.

How do I get around this?

Thanks.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
P

Peter Duniho

David said:
[...]
using (Image tempImage = Image.FromFile(FileName))

{

pictureBox1.Image = tempImage;

Refresh();

}

You should not need to call "Refresh()".
Now, that seemed to unlock the image and I could upload it and later delete
the file. However, if I attempt to scroll the panel with the picturebox
inside it, I get an error on the Application.Run line in my program.cs,
saying "A parameter is not valid".

Correct. You've disposed the image before your program is actually done
with it.
So, without the above using statement, my pic is still scrollable, but not
uploadable or deleteable. With the using statement, my pic is uploadable and
deleteable but not scrollable.

How do I get around this?

The correct way to deal with the "file locked" issue is to make a copy
of the image after loading it into memory, and then dispose the original
and work with the copy.

I believe that there are at least a couple of previous discussions in
this newsgroup describing various techniques for doing so. If you'd
like a more in-depth review, you should review those discussions. But
the most straightforward is probably just to pass the original Image to
the Bitmap constructor that duplicates a passed-in Image.

Pete
 

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