C#图像处理,进来帮帮忙,急啊!
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 图像处理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_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 > 225) rr = 255;
if (gg < 0) rr = 0;
if (gg> 225) rr = 255;
if (bb < 0) rr = 0;
if (bb > 225) rr = 255;
Color c1 = Color.FromArgb(rr, gg, bb);
box2.SetPixel(i, j, c1);
}
pictureBox2.Refresh();
pictureBox2.Image = box2;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
为什么运行时老是提示rr的值超过了255,哪里错了呢???怎么改? --------------------编程问答-------------------- rr > 225,应该是 rr > 255 吧。 --------------------编程问答-------------------- 不知道啊 --------------------编程问答-------------------- 首先你去看看是不是1楼说的原因
还有就是你的这句 rr = Math.Abs(r2 - r1 + 128);
你能确保rr的值会不超过255吗? --------------------编程问答--------------------
GetPixel(),坐标是从0开始算的,到边界后+1不报错么?
--------------------编程问答-------------------- rr = Math.Abs(r2 - r1 + 128);
cc = box1.GetPixel(i+1, j+1);
r2的范围0-255
r1的范围0-255
r2-r1的范围0-255
所以rr的范围有可能超过255,这种算法肯定是有问题的。 --------------------编程问答-------------------- rr = rr > 255?255:rr;
--------------------编程问答-------------------- for (int i = 0; i < pictureBox1.Width; i++)
应该是 box1.Width 吧 --------------------编程问答-------------------- 跟踪……查看值的变化。 --------------------编程问答-------------------- 学习文字不错!
补充:.NET技术 , C#