使用System.drawing时,C#参数无效 [英] C# Parameter is not valid occur when using System.drawing

查看:78
本文介绍了使用System.drawing时,C#参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在使用asp.net上传图片时遇到问题,很快解释是......

循环以获取HttpPostedFile中的每张图片。然后计算用于向其添加文本的颜色

错误将发生在System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myimage);的第二个或第三个循环上。



任何身体经历过此之前,请建议,非常感谢。



这是我的代码:

Hi All,

I've got a problem when uploading images using asp.net, shortly explain is...
Loop to get each picture in HttpPostedFile. Then calculate color for adding text to it
The error will occur on the second or some time third loop at "System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myimage);"

Any body had experienced through this before, please suggest, would be many thanks.

This is my codes:

private void saveFile(HttpPostedFile hpf, string strFileName)
        {
            System.Drawing.Image myimage = System.Drawing.Image.FromStream(hpf.InputStream, false, false);
            int imageSize = hpf.ContentLength;
            hpf = null;
            colorProcessing cp = averageColor(cropImage(myimage));
            addText(myimage,cp);
            
            myimage.Save(Server.MapPath("~/PlantsPhoto/") + strFileName);
            myimage.Dispose();
            System.Threading.Thread.Sleep(imageSize / 10);
        }

        public void addText(System.Drawing.Image myimage, colorProcessing cp)
        {

            var w = myimage.Width;
            var h = myimage.Height;
            double gaugeSize = h >= w ? h / 100 : w / 100;
            double fontSize = ((gaugeSize / 10) * (10 - (gaugeSize / 10))) + 4;
            System.Drawing.StringFormat strFormat = new System.Drawing.StringFormat();
            strFormat.Alignment = System.Drawing.StringAlignment.Near;
            strFormat.LineAlignment = System.Drawing.StringAlignment.Near;
            System.Drawing.Brush b= cp.colorTone!=0?System.Drawing.Brushes.White:System.Drawing.Brushes.Black;
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myimage);
            
            g.DrawString("xx©Copyright", new System.Drawing.Font("Tahoma", (int)fontSize), b,
                new System.Drawing.RectangleF(0, 0, w / 2, h / 2), strFormat);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.Dispose();
            myimage.Dispose();
            strFormat.Dispose();
            cp.Dispose();// = null;
            b.Dispose();
        }
        private System.Drawing.Bitmap cropImage(System.Drawing.Image img)
        {
            System.Drawing.Rectangle cropArea = new System.Drawing.Rectangle(0,0,img.Width / 2, img.Height/3);
            System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(img);
            System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea,bmpImage.PixelFormat);
            //bmpCrop.Save(Server.MapPath("~/PlantsPhoto/") + "ssss.jpg");
            bmpImage.Dispose();
            //img.Dispose();
            
            return bmpCrop;
        }
        public colorProcessing averageColor(System.Drawing.Bitmap img)
        {
            System.Drawing.Bitmap MyBitmap = img; 
            System.Drawing.Rectangle Rect = new System.Drawing.Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
            System.Drawing.Imaging.BitmapData BmpData = MyBitmap.LockBits(Rect,
                                               System.Drawing.Imaging.ImageLockMode.ReadOnly,
                                               MyBitmap.PixelFormat);


            int Size = BmpData.Stride * MyBitmap.Height;
            byte[] RGBs = new byte[Size];


            IntPtr Pointer = BmpData.Scan0;
            System.Runtime.InteropServices.Marshal.Copy(Pointer, RGBs, 0, Size);


            MyBitmap.UnlockBits(BmpData);


            // we've got all the RGB values in an array
            int Sum = 0;
            int whiteCount=0;
            for (int Index = 0; Index < RGBs.Length; Index++)
            {
                Sum += RGBs[Index];
                if (RGBs[Index]<50)
                {
                    whiteCount++;
                }
            }

            // higher Average means a lighter picture (255 = white, 0 = black)
            int Average = Sum / RGBs.Length;
            colorProcessing CPInfo = new colorProcessing();
            CPInfo.avgColor = Average;
            CPInfo.colorTone = (RGBs.Length / 15) > whiteCount ? colorProcessing.tone.White : colorProcessing.tone.Black;
            MyBitmap.Dispose();
            BmpData = null;
            RGBs = null;
            return CPInfo;
        }

推荐答案

我试着在system.drawing.graphics上使用关键字' .checkerrorstatus(int32 status)'直到我找到这个主题>> 实际上,当您使用System.Drawing.Brushes中的画笔时,问题是.NET Graphics对象中的错误。改为创建一个新的SolidBrush,我打赌问题会消失。
I've try to use keyword 'at system.drawing.graphics.checkerrorstatus(int32 status)' until I found this thread >> 'Actually the problem is a bug in the .NET Graphics object when you use a brush from System.Drawing.Brushes. Create a new SolidBrush instead and I bet the problem will go away.


这篇关于使用System.drawing时,C#参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆