Exception thrown: 'System.ArgumentException' in System.Drawing.dll

-2

I am trying to compress a bmp image and save in a desired location. But getting error

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

Also added the StackTrace logs

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);
            }
        }
    }

Added Compression Part

 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);
    }
c#
.net
bitmap
asked on Stack Overflow May 7, 2019 by Hariharan Gopal • edited May 7, 2019 by Hariharan Gopal

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0