我是靠谱客的博主 鲜艳音响,这篇文章主要介绍画图的相关问题,现在分享给大家,希望可以做个参考。

做一个相关的笔记

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 图像的处理
{

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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) { } }

}

最后

以上就是鲜艳音响最近收集整理的关于画图的相关问题的全部内容,更多相关画图内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部