标题:求助,请各位大大们帮忙
只看楼主
visit1452
Rank: 2
等 级:论坛游民
帖 子:7
专家分:10
注 册:2012-3-22
结帖率:0
已结贴  问题点数:20 回复次数:8 
求助,请各位大大们帮忙
[attach]712586[/attach]

  我做了一个跟美图秀秀那样可以对图片进行霓虹,灰度,浮雕等效果加工的程序。这是我的期末作业,但是就是有一点错误。我改了好久都没改出来,让教授看了,他看了十来分钟没弄好,只能请大家帮忙了,这个程序是在图书馆的一本C#书上的。全是那句红红色的错误。加上作品,方便大大们调试。[attach]712586[/attach]请大家指教。

using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace qmzy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        #region 打开图片
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofdlg = new OpenFileDialog();
            ofdlg.Filter = "Bmp File(*.bmp)|*.bmp";
            if (ofdlg.ShowDialog() == DialogResult.OK)
            {
                Bitmap image = new Bitmap(ofdlg.FileName);
                pictureBox1.Image = image;
            }
        }
        #endregion

 #region  霓虹
        private void button5_Click(object sender, EventArgs e)
        {
            Color c = new Color();
            Color cc = new Color();
            Color cd = new Color();
            Bitmap box1 = new Bitmap(pictureBox1.Image);
            Bitmap box2 = new Bitmap(pictureBox2.Image);
            int rr, gg, bb, r1, g1, b1, r2, g2, b2, rr1, rr2, gg1, gg2, bb1, bb2, r3, g3, b3;
            for (int i = 0; i < pictureBox1.Width; i++)
            {
                for (int j = 0; j < pictureBox1.Height; j++)
                {
                    rr = 0; gg = 0; bb = 0;
                    c = box1.GetPixel(i, j);
                    r1 = c.R;
                    g1 = c.G;
                    b1 = c.B;

                    cc = box1.GetPixel(i + 1, j);
                    r2 = cc.R;
                    g2 = cc.G;
                    b2 = cc.B;

                    cd = box1.GetPixel(i + 1, j);
                    r3 = cd.R;
                    g3 = cd.G;
                    b3 = cd.B;

                    rr1 = (r1 - r2) * (r1 - r2);
                    rr2 = (r1 - r3) * (r1 - r3);
                    rr = (int)(2 * Math.Sqrt(rr1 + rr2));
                    gg1 = (g1 - g2) * (g1 - g2);
                    gg2 = (g1 - g3) * (g1 - g3);
                    gg = (int)(2 * Math.Sqrt(gg1 + gg2));
                    bb1 = (b1 - b2) * (b1 - b2);
                    bb2 = (b1 - b3) * (b1 - b3);
                    bb = (int)(2 * Math.Sqrt(bb1 + bb2));



                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    if (bb < 0) bb = 0;
                    if (bb > 0) bb = 255;

                    Color c1 = Color.FromArgb(rr, gg, bb);
                    box2.SetPixel(i, j, c1);

                }

                pictureBox2.Refresh();    //刷新
                pictureBox2.Image = box2; //图片赋到图片框2中

            }
        }
       #endregion

        #region  平滑
        private void button6_Click(object sender, EventArgs e)
        {


            Color c = new Color();
            Bitmap box1 = new Bitmap(pictureBox1.Image);
            Bitmap box2 = new Bitmap(pictureBox2.Image);
            int rr, gg, bb, r1, g1, b1, rx, gx, bx, k1, k2;
            for (int i = 0; i < pictureBox1.Width - 1; i++)
            {
                for (int j = 0; j < pictureBox1.Height - 1; j++)
                {
                    rx = 0; gx = 0; bx = 0;
                    for (k1 = -1; k1 <= 1; k1++)
                    {
                        for (k2 = -1; k2 <= 1; k2++)
                        {
                            c = box1.GetPixel(i + k1, j + k2);
                            r1 = c.R;
                            g1 = c.G;
                            b1 = c.B;

                            rx = rx + r1;
                            gx = gx + g1;
                            bx = bx + b1;


                        }
                    }

                    rr = (int)(rx / 9);
                    gg = (int)(gx / 9);
                    bb = (int)(bx / 9);

                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    if (bb < 0) bb = 0;
                    if (bb > 0) bb = 255;

                    Color c1 = Color.FromArgb(rr, gg, bb);
                    box2.SetPixel(i, j, c1);


                }
                pictureBox2.Refresh();     //刷新
                pictureBox2.Image = box2;  //图片赋到图片框2中
            }

        }
        #endregion
      
        #region  灰度
        private void button2_Click(object sender, EventArgs e)
        {

            Color c = new Color();
            Bitmap box1 = new Bitmap(pictureBox1.Image);
            Bitmap box2 = new Bitmap(pictureBox2.Image);
            int rr, gg, bb, cc;
            for (int i = 0; i < pictureBox1.Width; i++)
            {
                for (int j = 0; j < pictureBox1.Height; j++)
                {

                    c = box1.GetPixel(i, j);
                    rr = c.B;
                    gg = c.G;
                    bb = c.B;
                    cc = (int)((rr + gg + bb) / 3);
                    if (cc < 0) cc = 0;
                    if (cc > 255) cc = 255;
                    Color c1 = Color.FromArgb(cc, cc, cc);
                    box2.SetPixel(i, j, c1);
                }
                pictureBox2.Refresh();     //刷新
                pictureBox2.Image = box2;  //图片赋到图片框2中

            }


        }
        #endregion

        #region  逆反
        private void button3_Click(object sender, EventArgs e)
        {
            Color c = new Color();
            Bitmap box1 = new Bitmap(pictureBox1.Image);
            Bitmap box2 = new Bitmap(pictureBox2.Image);
            for (int i = 0; i < pictureBox1.Width; i++)
            {
                for (int j = 0; j < pictureBox1.Height; j++)
                {
                    c = box1.GetPixel(i, j);
                    Color c1 = Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B);
                    box2.SetPixel(i, j, c1);
                }
                pictureBox2.Refresh();     //刷新
                pictureBox2.Image = box2;  //图片赋到图片框2中
            }


        }
        #endregion

        #region  锐化
        private void button4_Click(object sender, EventArgs e)
        {

            Color c = new Color();
            Color cc = new Color();
            Bitmap box1 = new Bitmap(pictureBox1.Image);
            Bitmap box2 = new Bitmap(pictureBox2.Image);
            int rr, gg, bb, r1, g1, b1, r2, g2, b2;
            for (int i = 0; i < pictureBox1.Width - 1; i++)
            {
                for (int j = 0; j < pictureBox1.Height - 1; j++)
                {
                    rr = 0; gg = 0; bb = 0;
                    c = box1.GetPixel(i, j);
                    r1 = c.R;
                    g1 = c.G;
                    b1 = c.B;

                    cc = box1.GetPixel(i - 1, j - 1);
                    r2 = cc.R;
                    g2 = cc.G;
                    b2 = cc.B;

                    rr = r1 + Math.Abs((r1 - r2) / 4);
                    gg = g1 + Math.Abs((g1 - g2) / 4);
                    bb = b1 + Math.Abs((b1 - b2) / 4);


                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    if (bb < 0) bb = 0;
                    if (bb > 0) bb = 255;

                    Color c1 = Color.FromArgb(rr, gg, bb);
                    box2.SetPixel(i, j, c1);

                }

                pictureBox2.Refresh();    //刷新
                pictureBox2.Image = box2; //图片赋到图片框2中

            }

        }  
        #endregion

        #region  浮雕
        private void button7_Click(object sender, EventArgs e)
        {
            Color c = new Color();
            Color cc = new Color();
            Bitmap box1 = new Bitmap(pictureBox1.Image);
            Bitmap box2 = new Bitmap(pictureBox2.Image);
            int rr, gg, bb, r1, g1, b1, r2, g2, b2;
            for (int i = 0; i < pictureBox1.Width; i++)
            {
                for (int j = 0; j < pictureBox1.Height; j++)
                {
                    rr = 0; gg = 0; bb = 0;
                    c = box1.GetPixel(i, j);
                    r1 = c.R;
                    g1 = c.G;
                    b1 = c.B;

                    cc = box1.GetPixel(i + 1, j + 1);
                    r2 = cc.R;
                    g2 = cc.G;
                    b2 = cc.B;

                    rr = Math.Abs(r2 - r1 + 128);
                    gg = g2 - g1 + 128;
                    bb = b2 - b1 + 128;

                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    if (bb < 0) bb = 0;
                    if (bb > 0) bb = 255;

                    Color c1 = Color.FromArgb(rr, gg, bb);
                    box2.SetPixel(i, j, c1);

                }

                pictureBox2.Refresh();    //刷新
                pictureBox2.Image = box2; //图片赋到图片框2中

            }

        }
        #endregion

        #region 镶嵌
        private void button8_Click(object sender, EventArgs e)
        {

            
            Color c = new Color();
            Bitmap box1 = new Bitmap(pictureBox1.Image);
            Bitmap box2 = new Bitmap(pictureBox2.Image);
            int rr, gg, bb, r1, g1, b1, rx, gx, bx, k1, k2;
qmzy.rar (476.21 KB)

            for (int i = 0; i < pictureBox1.Width - 1; i += 3)
            {
                for (int j = 0; j < pictureBox1.Height - 1; j += 3)
                {
                    rx = 0; gx = 0; bx = 0;
                    for (k1 = -1; k1 <= 3; k1++)
                    {
                        for (k2 = -1; k2 <= 3; k2++)
                        {
                            c = box1.GetPixel(i + k1, j + k2);
                            r1 = c.R;
                            g1 = c.G;
                            b1 = c.B;

                            rx = rx + r1;
                            gx = gx + g1;
                            bx = bx + b1;

                        }
                    }

                    rr = (int)(rx / 25);
                    gg = (int)(gx / 25);
                    bb = (int)(bx / 25);

                    if (rr < 0) rr = 0;
                    if (rr > 255) rr = 255;
                    if (gg < 0) gg = 0;
                    if (gg > 255) gg = 255;
                    if (bb < 0) bb = 0;
                    if (bb > 0) bb = 255;

                    for (k1 = 0; k1 <= 3; k1++)
                    {
                        for (k2 = 0; k2 <= 3; k2++)
                        {
                            Color c1 = Color.FromArgb(rr, gg, bb);
                            box2.SetPixel(i, j, c1);

                        }

                    }
                    pictureBox2.Refresh();    //刷新
                    pictureBox2.Image = box2; //图片赋到图片框2中

                }
            }


        }  
        #endregion
搜索更多相关主题的帖子: 美图秀秀 
2012-05-27 12:35
visit1452
Rank: 2
等 级:论坛游民
帖 子:7
专家分:10
注 册:2012-3-22
得分:0 
版主,帮帮忙撒
2012-05-27 22:56
zwffff
Rank: 4
等 级:业余侠客
威 望:1
帖 子:58
专家分:224
注 册:2007-11-13
得分:10 
问题是什么?报什么错??

欢迎加入.Net技术交流群:90925122
2012-05-28 00:57
zwffff
Rank: 4
等 级:业余侠客
威 望:1
帖 子:58
专家分:224
注 册:2007-11-13
得分:0 
这句Bitmap box2 = new Bitmap(pictureBox2.Image);里面的pictureBox2.Image在哪里赋值了? 是null值

欢迎加入.Net技术交流群:90925122
2012-05-28 01:07
visit1452
Rank: 2
等 级:论坛游民
帖 子:7
专家分:10
注 册:2012-3-22
得分:0 
回复 2楼 visit1452
就是用红颜色的那一句错误,就是赋值为null,然后该过的图片传不过去。但是第一句pictureBox1就可以
2012-05-28 08:13
visit1452
Rank: 2
等 级:论坛游民
帖 子:7
专家分:10
注 册:2012-3-22
得分:0 
回复 4楼 zwffff
对啊,就是这点问题。不能赋值到pictureBox2,但是就是找不到办法解决,我又建了一个pictureBox3,还是为null,只有pictureBox1是可以的
2012-05-28 08:15
zhangd
Rank: 2
等 级:论坛游民
帖 子:13
专家分:58
注 册:2012-5-17
得分:10 
啥地方
斯蒂芬森的

[ 本帖最后由 zhangd 于 2012-5-28 10:42 编辑 ]
2012-05-28 10:20
zhangd
Rank: 2
等 级:论坛游民
帖 子:13
专家分:58
注 册:2012-5-17
得分:0 
回复 楼主 visit1452
我已经帮你弄好了,如果需要可以发邮件给我,zhangd512@
收到的鲜花
  • visit14522012-05-29 22:53 送鲜花  5朵   附言:真的好了,谢谢了
2012-05-28 10:43
zwffff
Rank: 4
等 级:业余侠客
威 望:1
帖 子:58
专家分:224
注 册:2007-11-13
得分:0 
这句Bitmap box2 = new Bitmap(pictureBox2.Image);
替换成Bitmap box2 = new Bitmap(pictureBox1.Image);
收到的鲜花
  • visit14522012-05-29 22:53 送鲜花  2朵   附言:是的,是的,谢谢

欢迎加入.Net技术交流群:90925122
2012-05-28 21:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-368108-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.265043 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved