Click here to Skip to main content
15,889,034 members
Home / Discussions / C#
   

C#

 
AnswerRe: c# help Pin
dan!sh 8-Sep-09 8:29
professional dan!sh 8-Sep-09 8:29 
AnswerRe: c# help Pin
Christian Graus8-Sep-09 11:37
protectorChristian Graus8-Sep-09 11:37 
AnswerRe: c# help Pin
April Fans22-Sep-09 19:11
April Fans22-Sep-09 19:11 
Questionthanks Pin
tanweer8-Sep-09 6:47
tanweer8-Sep-09 6:47 
Questionrecord and play mp4 file Pin
Markandaiya Harsh8-Sep-09 6:27
Markandaiya Harsh8-Sep-09 6:27 
AnswerRe: record and play mp4 file Pin
Henry Minute8-Sep-09 10:57
Henry Minute8-Sep-09 10:57 
QuestionOverwrite an image Pin
vandel2128-Sep-09 5:55
vandel2128-Sep-09 5:55 
AnswerRe: Overwrite an image [modified] Pin
Luc Pattyn8-Sep-09 6:08
sitebuilderLuc Pattyn8-Sep-09 6:08 
Hi,

a likely cause for bmpPicture.Save(FilePath, ImageFormat.Bmp); to throw an exception is the file is locked "by another process", which typically means your own process (or maybe some other) has the file open. Typically you would have loaded it either with Image.FromFile() or PictureBox.Load()

Images loaded that way keep their file open for as long as they exist. There are three remedies:

1. quick and dirty
get the picture, make sure you don't need it anymore, call dispose:
Image img=PictureBox.Image;
PictureBox.Image=null;
img.Dispose();
img=null;
// now the original file is unlocked unless you are still using the image somewhere else...


2. quick and dirty
when you initially get the image, make a copy and throw away the original:
Bitmap bm2=null;
{
    Bitmap bm1=Image.FromFile("image.jpeg");
    bm2=new Bitmap(bm1);
    bm1.Dispose();
}
// now use bm2, the file is unlocked (bm1 is out of scope already!)


3. clean and easy
don't use Image.FromFile nor PictureBox.Load; instead use Image.FromStream
Bitmap bm=Image.FromStream(File.OpenRead("image.jpeg"));


[EDIT] Bug fixed in my other reply ]/EDIT]

Smile | :)

Luc Pattyn

Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.


modified on Tuesday, September 8, 2009 1:53 PM

GeneralRe: Overwrite an image Pin
harold aptroot8-Sep-09 6:10
harold aptroot8-Sep-09 6:10 
AnswerRe: Overwrite an image Pin
Luc Pattyn8-Sep-09 7:58
sitebuilderLuc Pattyn8-Sep-09 7:58 
QuestionTaking a snapshot from a local webcam... Pin
to_aru_shiroi_neko8-Sep-09 5:37
to_aru_shiroi_neko8-Sep-09 5:37 
QuestionWorking with very large numbers. Pin
JTKRIST8-Sep-09 5:13
JTKRIST8-Sep-09 5:13 
AnswerRe: Working with very large numbers. Pin
Luc Pattyn8-Sep-09 5:22
sitebuilderLuc Pattyn8-Sep-09 5:22 
AnswerRe: Working with very large numbers. Pin
Richard MacCutchan8-Sep-09 5:26
mveRichard MacCutchan8-Sep-09 5:26 
AnswerRe: Working with very large numbers. Pin
harold aptroot8-Sep-09 5:39
harold aptroot8-Sep-09 5:39 
AnswerRe: Working with very large numbers. Pin
DaveyM698-Sep-09 5:40
professionalDaveyM698-Sep-09 5:40 
QuestionHow to notify all instance of a class that a change is happened Pin
Nitheesh George8-Sep-09 4:48
Nitheesh George8-Sep-09 4:48 
AnswerRe: How to notify all instance of a class that a change is happened Pin
monstale8-Sep-09 5:16
monstale8-Sep-09 5:16 
Questioncompile Pin
sanforjackass8-Sep-09 4:27
sanforjackass8-Sep-09 4:27 
AnswerRe: compile Pin
stancrm8-Sep-09 4:30
stancrm8-Sep-09 4:30 
AnswerRe: compile Pin
Manas Bhardwaj8-Sep-09 4:39
professionalManas Bhardwaj8-Sep-09 4:39 
Questionis there any way to create a new Process from a Stream? [modified] Pin
Demasoni8-Sep-09 3:51
Demasoni8-Sep-09 3:51 
GeneralRe: is there any way to create a new Process from a Stream? Pin
harold aptroot8-Sep-09 3:55
harold aptroot8-Sep-09 3:55 
QuestionRe: is there any way to create a new Process from a Stream? Pin
OriginalGriff8-Sep-09 3:56
mveOriginalGriff8-Sep-09 3:56 
AnswerRe: is there any way to create a new Process from a Stream? Pin
Dave Kreskowiak8-Sep-09 4:39
mveDave Kreskowiak8-Sep-09 4:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.