马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

总是提示字段初始值无法引用非静态字段

[复制链接]
查看4596 | 回复1 | 2013-10-10 16:17:15 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing ;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap bmp1;
        private void 打开图像_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                bmp1 = (Bitmap)Image.FromFile(openFileDialog1.FileName);
            }
            pictureBox1.Image = bmp1;
            
        }
        public static Bitmap Cut(Bitmap b)
        {
            if (b == null)
            {
                return null;
            }
            int w = b.Width;
            int h = b.Height;
            int StartX = (w / 2) - 500;
            int StartY = (h / 2) - 500;
            int endX = (w / 2) + 500;
            int endY = (w / 2) + 500;
            int iWidth = 1000;
            int iHeight = 1000;
            Bitmap bitOut = new Bitmap(iWidth, iHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            Graphics g = Graphics.FromImage(bitOut);
            g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, endX, endY), GraphicsUnit.Pixel);
            g.Dispose();
            return bitOut;

        }
        Bitmap bmp2 = Cut(bmp1);\\总是提示字段初始值无法引用非静态字段
        private void button1_Click(object sender, EventArgs e)
        {
            Rectangle rect = new Rectangle(0, 0, bmp2.Width, bmp2.Height);
            //以可读的方式锁定全部位图像素
            System.Drawing.Imaging.BitmapData bmpData = bmp2.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp2.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = bmpData.Stride * bmpData.Height;
            byte[] rgbValues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            //灰度化
            double colorTemp = 0;
            //for (int i = 0; i < rgbValues.Length; i += 3)//对24位图像
            //{
            //    colorTemp = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues * 0.114;
            //    rgbValues = rgbValues[i + 1] = rgbValues[i + 2] = (byte)colorTemp;
            //}
            double R, G, B;
            int n;
            for (int i = 0; i < bmpData.Height; i++)
            {
                //只处理每行中是图像像素的数据,舍弃未用空间。
                for (int j = 0; j < bmpData.Width * 3; j += 3)
                {
                    colorTemp = rgbValues[i * bmpData.Stride + j + 2] * 0.299 +  + rgbValues[i * bmpData.Stride + j] * 0.114;
                    R = rgbValues[i * bmpData.Stride + j + 2];
                    G = rgbValues[i * bmpData.Stride + j + 1] ;
                    B = rgbValues[i * bmpData.Stride + j];
                    if(R<G&&B<G)
                    {
                        if(R>40&&R<100)
                        {
                            R=G=B=0;
                            n++;
                        }
                    }
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
            bmp1.UnlockBits(bmpData);
            Invalidate();
            pictureBox2.Image = bmp1;
            text1.Text = (n / (bmp2.Width * bmp2.Height)).ToString();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        
      
      
   
    }
   
}

ibcadmin | 2013-10-10 16:38:48 | 显示全部楼层
Cut()方法返回的应该是null  你调试看看返回结果  如果不是null  那就把static 删掉 再试试
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则