c# 报错 参数无效

.Net技术 码拜 9年前 (2015-05-10) 2721次浏览 0个评论
 

我 把扫描仪扫到的一些图片保存到本地之后读取,因为有很多所以 加了一 FlowLayoutPanel 控件
在flp控件上有加了一个panel控件,在panel控件上 有加了一个picturebox控件,当我点击picturebox  时,获取image,赋给我页面上的另一个picturebox(显示大小图片),在我显示大小图片的这个pic控件上我做了一个左右旋转的功能,但是当我左右旋转的时候图片是旋转的,但是过一会他就会 报错 ,提示我 参数无效?
因为我 引用了两个第三方 的dll,我想问一下,是我引dll的问题吗??一下是 报错 信息

2013-06-18 13:51:10    参数无效。
   在 System.Drawing.Image.get_Width()
   在 System.Drawing.Image.get_Size()
   在 System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
   在 System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   在 System.Windows.Forms.Control.WmPaint(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.Run(Form mainForm)
   在 GreatHandFFs.UI.Program.Main() 位置 C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\GreatHandFFs.root\GreatHandFFs\GreatHandFFs.UI\Program.cs:行号 51

求大神 指点 一下 ,异常的source  是 System.Drawing

30分
肯定是跟读取图片信息有关,你好好跳是不,一步步旋转,看看什么条件下报错
15分
认真检查每一步,肯定有细节问题!
20分
请检查看看,如果是不是已经image.Dispose();释放了,如果释放了,就把//image.Dispose();这个注释掉看看。
引用 3 楼 qingfeng_wu 的回复:

请检查看看,如果是不是已经image.Dispose();释放了,如果释放了,就把//image.Dispose();这个注释掉看看。

恩,但是如果  我 不释放的话 比如说 ,我显示大小图片的pic是处于在 旋转的情况下的话,我在点击 flp控件上的pic的话他还是回报 参数无效,
现在的问题是 只要 已操作 旋转 事件  就 报参数无效 

10分
建议贴出来你旋转的代码,以及出错的代码。
从错误信息上只能看出来是在获取对象宽度的时候出错了!
 #region 图片旋转函数
        /// <summary>
        /// 以逆时针为方向对图像进行旋转
        /// </summary>
        /// <param name="b">位图流</param>
        /// <param name="angle">旋转角度[0,360](前台给的)</param>
        /// <returns></returns>
        public static Image RotateImg(Image b, int angle)
        {
            angle = angle % 360;

            //弧度转换
            double radian = angle * Math.PI / 180.0;
            double cos = Math.Cos(radian);
            double sin = Math.Sin(radian);

            //原图的宽和高
            int w = b.Width;
            int h = b.Height;
            int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
            int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));

            //目标位图
            Bitmap dsImage = new Bitmap(W, H);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //计算偏移量
            Point Offset = new Point((W - w) / 2, (H - h) / 2);

            //构造图像显示区域:让图像的中心与窗口的中心点一致
            Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
            Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);

            g.TranslateTransform(center.X, center.Y);
            g.RotateTransform(360 - angle);

            //恢复图像在水平和垂直方向的平移
            g.TranslateTransform(-center.X, -center.Y);
            g.DrawImage(b, rect);

            //重至绘图的所有变换
            g.ResetTransform();
            g.Save();
            g.Dispose();
            //保存旋转后的图片
            b.Dispose();
            //dsImage.Save("FocusPoint.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            return dsImage;
        }

以上旋转代码

private void pb_Click(object sender, EventArgs e)
        {
            try
            {
                if (pbBigImage.Image != null&&pbBigImage!=null) 
                {
                    pbBigImage.Image.Dispose();
                    pbBigImage.Dispose();
                }
                
                PictureBox pb = sender as PictureBox;
                picfs = 1;
                pbBigImage.Tag = pb.Tag;
                
                pbBigImage.SizeMode = PictureBoxSizeMode.Zoom;
                pbBigImage.Size = new Size(368, 441);
                picHeight = pbBigImage.Size.Height;
                picWidth = pbBigImage.Size.Width;
                pbBigImage.Image = pb.Image;
            }
            catch (Exception )
            {
                
            }
        }

以上点击panel里的pic 给 显示图片大小的pic赋值

25分
你可以这样操作看看,等你把所以的事情都做完后,在一一的Dispose()释放资源。没有完成的时候,可以先别急这Dispose(),看看还会不会出现这种问题。
引用 7 楼 qingfeng_wu 的回复:

你可以这样操作看看,等你把所以的事情都做完后,在一一的Dispose()释放资源。没有完成的时候,可以先别急这Dispose(),看看还会不会出现这种问题。

您说的是 什么意思啊 ,我 怎么知道 用户在什么时候点击 旋转 和 点击 其他啊 

如果说,你的函数是分开的话,应该不会出现这种问题。应该是Dispose()或者其它资源释放了照成的原因。再找找吧
引用 9 楼 qingfeng_wu 的回复:

如果说,你的函数是分开的话,应该不会出现这种问题。应该是Dispose()或者其它资源释放了照成的原因。再找找吧

就是图片旋转方法 里的 dispose()方法造成的,我 如果 不释放 那个方法的dispose() 会有什么 影响吗?

b.Dispose()

是这句引起的?

引用 10 楼 l397870376 的回复:
Quote: 引用 9 楼 qingfeng_wu 的回复:

如果说,你的函数是分开的话,应该不会出现这种问题。应该是Dispose()或者其它资源释放了照成的原因。再找找吧

就是图片旋转方法 里的 dispose()方法造成的,我 如果 不释放 那个方法的dispose() 会有什么 影响吗?

现在工具的强大,即使你不释放,它也有自己的回收机制,所以说,为什么现在程序员越来越笨就是这样。如果能自己释放是最好不过的。

我也遇到相同的问题,通过以下两个步骤解决
1、在方法体内部使用克隆后的Image,不再访问源Image
2、在方法体内部外括lock(…){ … }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c# 报错 参数无效
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!