引发异常:System.Drawing.dll

时间:2019-05-07 18:58:08

标签: c# .net bitmap

我正在尝试压缩bmp图像并保存在所需位置。但是出错了

引发的异常:System.Drawing.dll中的“ System.ArgumentException” System.Drawing.dll中发生类型为'System.ArgumentException'的未处理异常 参数无效。

还添加了StackTrace日志

System.ArgumentException
HResult=0x80070057
Message=Parameter is not valid.
Source=System.Drawing

StackTrace:
at System.Drawing.Bitmap..ctor(String filename)
at IPLab.MainForm.compress() in 
C:\Users\Hariharan\Desktop\code\MainForm.cs:line 1495
at IPLab.MainForm.mainToolBar_ButtonClick(Object sender, 
ToolBarButtonClickEventArgs e) in 
C:\Users\Hariharan\Desktop\code\MainForm.cs:line 905
at System.Windows.Forms.ToolBar.OnButtonClick(ToolBarButtonClickEventArgs 
e)
at System.Windows.Forms.ToolBar.WmReflectCommand(Message& m)
at System.Windows.Forms.ToolBar.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, 
Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, 
IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, 
Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Application.ParkingWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, 
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, 
Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ToolBar.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at IPLab.MainForm.Main() in 
C:\Users\Hariharan\Desktop\code\MainForm.cs:line 749

Compress()

 private void compress()
    {
        Content doc = dockManager.ActiveDocument;
        if (doc != null)
        {
            // string ImagePath = @"C:\Users\Hariharan\Desktop\MARBLES.jpg";

            folderBrowserDialog1.ShowDialog();

            string newImagePath = folderBrowserDialog1.SelectedPath;

            string ImagePath = Path.GetFileName(ofd.FileName).ToString();

            using (Bitmap ImgTemp = new Bitmap(ImagePath))
            {
                //ImgTemp = new Bitmap(ImagePath);

                SaveJPGWithCompressionSetting(ImgTemp, newImagePath + ImagePath, 40L);
            }
        }
    }

添加了压缩部分

 private void SaveJPGWithCompressionSetting(System.Drawing.Image image, string szFileName, long lCompression)
{
    EncoderParameters eps = new EncoderParameters(1);
        eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, lCompression);
        ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
        image.Save(szFileName, ici, eps);
    }

0 个答案:

没有答案