求助,请各位大大们帮忙
[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