概述
做一个相关的笔记
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 图像的处理
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Bitmap curbitmap=null;
private string curfileName=null;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog OpenDlg = new OpenFileDialog();
OpenDlg.Filter = "所有图像文件|*.bmp;*.jpg;*.png;*.gif;*.pcx;";
OpenDlg.Title = "打开图像文件";
// OpenDlg.ShowHelp = true;
if (OpenDlg.ShowDialog()==DialogResult.OK)
{
//获取文件的名称//文件的路径
curfileName = OpenDlg.FileName;
//创建图像对象
try
{
curbitmap = (Bitmap)Image.FromFile(curfileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Invalidate();
//刷新图片
//fulshMap(curbitmap);
}
private void fulshMap(Bitmap map)
{
Graphics g = this.CreateGraphics();
// g.Clear(Color.White);
if (map != null)
{
float m = 600;
float k = map.Height / m;
g.DrawImage(map, 160, 20, map.Width / k, m);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (curbitmap == null) return;
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Title = "保存为:";
saveDlg.OverwritePrompt = true;
if (saveDlg.ShowDialog() == DialogResult.OK)
{
string fileName = saveDlg.FileName;
// MessageBox.Show(fileName);
//获取文件的扩展名
string ExtnName= fileName.Substring(fileName.LastIndexOf('.')+1);
switch (ExtnName)
{
case "bmp":
curbitmap.Save(fileName,System.Drawing.Imaging.ImageFormat.Bmp);
break;
case "jpg":
curbitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case "gif":
curbitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Gif);
break;
case "png":
curbitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
break;
case "tif":
curbitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Tiff);
break;
default:
break;
}
MessageBox.Show("图片保存成功!:"+fileName);
}
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (curbitmap != null)
{
float m = 600;
float k = curbitmap.Height / m;
g.DrawImage(curbitmap, 160, 20, curbitmap.Width / k, m);
}
}
private void button6_Click(object sender, EventArgs e)
{
Task t1 = Task.Run(()=> getPix());
}
private void getPix()
{
Stopwatch sw = new Stopwatch();
sw.Start();
int t = 0;
if (curbitmap != null)
{
Color curcolor;
int ret;
for (int i = 0; i < curbitmap.Width; i++)
{
t++;
for (int j = 0; j < curbitmap.Height; j++)
{
curcolor = curbitmap.GetPixel(i, j);
ret = (int)(curcolor.R * 0.2999 + curcolor.G * 0.587 + curcolor.B * 0.114);
curbitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret));
}
textBox1.Invoke(new Action(() =>
{
textBox1.Text = t.ToString();
}));
}
fulshMap(curbitmap);
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
}
}
private void memory()
{
if (curbitmap != null)
{
Rectangle rect = new Rectangle(0,0,curbitmap.Width,curbitmap.Height);
System.Drawing.Imaging.BitmapData bmpData = curbitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curbitmap.PixelFormat);
IntPtr ptr= bmpData.Scan0;
int length = bmpData.Stride * bmpData.Height;
byte[] buffers = new byte[length];
System.Runtime.InteropServices.Marshal.Copy(ptr,buffers,0,length);
int k = 0;
//灰度化
double colorTemp = 0;
try
{
for (int i = 0; i < bmpData.Height; i++)
{
k++;
for (int j = 0; j < bmpData.Width * 3; j += 3)
{
colorTemp = buffers[i * bmpData.Stride + j] * 0.2999 + buffers[i * bmpData.Stride + j + 1] * 0.587 + buffers[i * bmpData.Stride + j + 2] * 0.114;
buffers[i * bmpData.Stride + j + 2] = buffers[i * bmpData.Stride + j + 1] = buffers[i * bmpData.Stride + j] = (byte)colorTemp;
}
textBox1.Invoke(new Action(() =>
{
textBox1.Text = k.ToString();
}));
}
System.Runtime.InteropServices.Marshal.Copy(buffers, 0, ptr, length);
curbitmap.UnlockBits(bmpData);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
Invalidate();
textBox1.Invoke(new Action(() =>
{
textBox1.Text = "1";
}));
}
}
private void button5_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
sw.Start();
if (curbitmap != null)
{
Rectangle rec = new Rectangle(0,0,curbitmap.Width,curbitmap.Height);
//获取像素,并锁定在内存中
BitmapData bmpData = curbitmap.LockBits(rec,ImageLockMode.ReadWrite,curbitmap.PixelFormat);
int length = bmpData.Stride * bmpData.Height;
byte[] buffers = new byte[length];
//扫描进内存中
System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0,buffers,0,length);
for (int i = 0; i < bmpData.Height; i++)
{
for (int j = 0; j < bmpData.Width * 3; j += 3)//每个里存储3个数组
{
double colorTemp = buffers[i * bmpData.Stride + j] * 0.2999 + buffers[i * bmpData.Stride + j + 1] * 0.587 + buffers[i * bmpData.Stride + j + 2] * 0.114;
buffers[i * bmpData.Stride + j + 2] = buffers[i * bmpData.Stride + j + 1] = buffers[i * bmpData.Stride + j] = (byte)colorTemp;
}
}
System.Runtime.InteropServices.Marshal.Copy(buffers,0,bmpData.Scan0,length);
curbitmap.UnlockBits(bmpData);//解锁
//刷新图片
//fulshMap(curbitmap);
Invalidate();
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
最后
以上就是鲜艳音响为你收集整理的画图的相关问题的全部内容,希望文章能够帮你解决画图的相关问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复