基本信息
源码名称:C# 图像处理之灰度(去颜色的三种方法)
源码大小:0.19M
文件格式:.rar
开发语言:C#
更新时间:2019-06-15
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
去颜色的三种方法:内存法、指针法、像素提取法
去颜色的三种方法:内存法、指针法、像素提取法
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.Diagnostics;
namespace ImageProcessing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stopwatch sw = new Stopwatch();
Bitmap bitmap;
Bitmap newbitmap;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
bitmap = (Bitmap)Image.FromFile(path);
pictureBox1.Image = bitmap.Clone() as Image;
}
}
private void button4_Click(object sender, EventArgs e)
{
if (bitmap != null)
{
newbitmap = bitmap.Clone() as Bitmap;
sw.Reset();
sw.Start();
Color pixel;
int ret;
for (int x = 0; x < newbitmap.Width; x )
{
for (int y = 0; y < newbitmap.Height; y )
{
pixel = newbitmap.GetPixel(x, y);
ret = (int)(pixel.R * 0.299 pixel.G * 0.587 pixel.B * 0.114);
newbitmap.SetPixel(x, y, Color.FromArgb(ret, ret, ret));
}
}
sw.Stop();
label1.Text = sw.ElapsedMilliseconds.ToString();
pictureBox2.Image = newbitmap.Clone() as Image;
}
}
private void button5_Click(object sender, EventArgs e)
{
if (bitmap != null)
{
newbitmap = bitmap.Clone() as Bitmap;
sw.Reset();
sw.Start();
Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height);
System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat);
IntPtr ptr = bmpdata.Scan0;
int bytes = newbitmap.Width * newbitmap.Height * 3;
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)
{
colortemp = rgbvalues[i 2] * 0.299 rgbvalues[i 1] * 0.587 rgbvalues[i] * 0.114;
rgbvalues[i] = rgbvalues[i 1] = rgbvalues[i 2] = (byte)colortemp;
}
System.Runtime.InteropServices.Marshal.Copy(rgbvalues, 0, ptr, bytes);
newbitmap.UnlockBits(bmpdata);
sw.Stop();
label2.Text = sw.ElapsedMilliseconds.ToString();
pictureBox2.Image = newbitmap.Clone() as Image;
}
}
private void button6_Click(object sender, EventArgs e)
{
if (bitmap != null)
{
newbitmap = bitmap.Clone() as Bitmap;
sw.Reset();
sw.Start();
Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height);
System.Drawing.Imaging.BitmapData bmpdata = newbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, newbitmap.PixelFormat);
byte temp;
unsafe
{
byte* ptr = (byte*)(bmpdata.Scan0);
for (int x = 0; x < bmpdata.Width; x )
{
for (int y = 0; y < bmpdata.Height; y )
{
temp = (byte)(0.299 * ptr[2] 0.587 * ptr[1] 0.114 * ptr[0]);
ptr[0] = ptr[1] = ptr[2] = temp;
ptr = 3;
}
ptr = bmpdata.Stride - bmpdata.Width * 3;
}
}
newbitmap.UnlockBits(bmpdata);
sw.Stop();
label3.Text = sw.ElapsedMilliseconds.ToString();
pictureBox2.Image = newbitmap.Clone() as Image;
}
}
}
}