I have a program where the users can save an attachment for a contract. The attachment is saved in the database a a varbinary. The users can then view the attachment.
I am saving the file to a temp location while they view it and then deleting the file after the process is closed. This is working fine for word and excel documents but not for jpeg files.
File.WriteAllBytes(@"C:\Temp\" + SelectedAttachment.FileName + SelectedAttachment.FileType, SelectedAttachment.FileBlob);
//open the file for viewing
var attachmentProcess = System.Diagnostics.Process.Start(@"C:\Temp\" + SelectedAttachment.FileName + SelectedAttachment.FileType);
attachmentProcess.WaitForExit();
//Delete temp file after user closes the file
File.Delete(@"C:\Temp\" + SelectedAttachment.FileName + SelectedAttachment.FileType);
The attachmentProcess.WaitForExit(); throws an 'System.NullReferenceException' but was not handled in user code error when the .jpeg opens. The file will open regardless of the error but does not delete upon closing the file.
I have tried using
using (Image image = Image.FromStream(new MemoryStream(SelectedAttachment.FileBlob)))
{
image.Save("output.jpg", ImageFormat.Jpeg);
}
but i get an error stating that " 'Image': type used in a using statement must be implicitly convertible to 'System.IDisposable'
Is there a way to get the .jpeg file process to behave like the word or excel files or should I use the MemoryStream route and if so what am i doing wrong there?