Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从字节数组打开jpeg_C#_Image_Bytearray_Jpeg - Fatal编程技术网

C# 从字节数组打开jpeg

C# 从字节数组打开jpeg,c#,image,bytearray,jpeg,C#,Image,Bytearray,Jpeg,我有一个程序,用户可以保存合同附件。附件以varbinary格式保存在数据库中。然后,用户可以查看附件 我在他们查看文件时将文件保存到临时位置,然后在进程关闭后删除该文件。这适用于word和excel文档,但不适用于jpeg文件 File.WriteAllBytes(@"C:\Temp\" + SelectedAttachment.FileName + SelectedAttachment.FileType, SelectedAttachment.FileBlob);

我有一个程序,用户可以保存合同附件。附件以varbinary格式保存在数据库中。然后,用户可以查看附件

我在他们查看文件时将文件保存到临时位置,然后在进程关闭后删除该文件。这适用于word和excel文档,但不适用于jpeg文件

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);
attachmentProcess.WaitForExit()文件;引发“System.NullReferenceException”,但在.jpeg打开时未在用户代码错误中处理。无论出现何种错误,文件都将打开,但在关闭文件时不会删除

我试过使用

using (Image image = Image.FromStream(new MemoryStream(SelectedAttachment.FileBlob)))
                {
                    image.Save("output.jpg", ImageFormat.Jpeg);  
                }
但我得到一个错误,指出“'Image':using语句中使用的类型必须隐式转换为'System.IDisposable'

有没有办法使.jpeg文件进程的行为类似于word或excel文件,或者我应该使用MemoryStream路由?如果有,我做错了什么?

您正在使用进程的.Start方法并传入文件名(由于它是一个图像,我猜它被视为一个url,并通过file:///your-file-path/file-name)。这实际上返回null而不是进程,因此您会出错。您应该将程序名(例如web浏览器)和文件名作为参数传递(使用该方法的版本)

另一方面,考虑创建一个进程对象(通过新的关键字,很可能封装在一个使用语句中),并用给定的StistFipe属性调用该实例上的启动方法。这将确保您有一个实际的进程对象等待退出。

谷歌“Process。Ststart返回null”。谷歌“Studio.Dig.IGIN”映像。“。可能的副本