JpegBitmapDecoder中的参数无效错误

时间:2018-06-04 07:12:18

标签: c# bitmap system.drawing

错误

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
Additional information: Parameter is not valid.

在行

中发生

decoder = new JpegBitmapDecoder(stream, bitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);

堆栈跟踪

at System.Drawing.Image.get_Width()
at System.Drawing.Image.get_Size()
at System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  

程序对JPEG文件组执行图像处理。

从GetWindowsTags和SetWindowsTags重复调用包含该行的函数(针对不同的图像),并且在可变数量的调用之后发生故障,但有时更快地使用较大的图像,这意味着存储器问题。但是,使用任务管理器进行监视显示重复调用时内存使用没有系统性增加。错误的不可重复性(它不能由try-catch结构处理)使得很难看出是什么导致它。

JpegMetadataAdapter 类中的完整功能

private BitmapFrame getBitmapFrame(string path)
    {
        BitmapDecoder decoder = null;
        using (Stream stream = System.IO.File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
        {
            decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
        }
        BitmapFrame bf = decoder.Frames[0];
        decoder= null;           
        return bf;
    }

调用函数getBitmapFrame
public JpegMetadataAdapter(string path)
    {
        this.path = path;
        frame = getBitmapFrame(path);

        Metadata = (BitmapMetadata)frame.Metadata.Clone();
        //Metadata = (BitmapMetadata)frame.Metadata;
    }

元素由函数

检索
public static void GetWindowsTags(string sFile, ref string Title, ref string Subject, ref string Comment, bool bShowError = false)
    {
            Cursor.Current = Cursors.WaitCursor;
            JpegMetadataAdapter jpeg = new JpegMetadataAdapter(sFile);
            Comment = (jpeg.Metadata.Comment + "").Trim();
            Title = (jpeg.Metadata.Title + "").Trim();
            Subject = (jpeg.Metadata.Subject + "").Trim();                              
            return;

    }

并由函数

设置
public static void SetWindowsTags(string sFile, string sFileOut, string Title = "", string Subject = "", string Comment = "", bool bShowError = false)
    {
            var jpeg = new JpegMetadataAdapter(sFile);               
            jpeg.Metadata.Comment = Comment;
            jpeg.Metadata.Title = Title;
            jpeg.Metadata.Subject = Subject;
            jpeg.SaveAs(sFileOut);               
            return;
        }

GetWindowsTags在每个输入文件和SetWindowsTags的处理循环中被调用3次

以下代码有时会导致内存不足'错误,但不可重复。字符串数组LocalFilesToShow 包含118个JPEG文件路径。这种行为非常奇怪,可能表明我的Win 10机器上的System.Drawing.dll存在问题。

//test of error in GetWindowsTags
                List<string> SearchResults = new List<string>();
                 for (int i = 0;  i < LocalFilesToShow.GetUpperBound(0); i++)
                {
                    string sTitle = "";
                    string sSubject = "";
                    string sComment = "";
                    GetWindowsTags(LocalFilesToShow[i], ref sTitle, ref sSubject, ref sComment);
                    if ((sTitle+ sSubject).IndexOf("NT") != 0)SearchResults.Add(LocalFilesToShow[i]);                      
                }`

0 个答案:

没有答案