来了,顶一下,评论下,我会开心很多。
这边还是以前留下的代码片段,1,8,24位图片,你变我啊,我变你的。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace TypeConverter 11 { 12 using System.Diagnostics; 13 using System.Drawing.Imaging; 14 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 22 private void button1_Click(object sender, EventArgs e) 23 { 24 var bmp = Image.FromFile("test1.tif") as Bitmap; 25 26 var w = bmp.Width; 27 var h = bmp.Height; 28 29 var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, 30 PixelFormat.Format1bppIndexed); 31 32 var bmpDest = new Bitmap(w, h, PixelFormat.Format8bppIndexed); 33 var dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, 34 PixelFormat.Format8bppIndexed); 35 unsafe 36 { 37 for (int y = 0; y < h; y++) 38 { 39 for (int x = 0; x < w; x++) 40 { 41 byte* pDest = (byte*)dataDest.Scan0 + x + y * dataDest.Stride; 42 byte* p = (byte*)data.Scan0 + x/8 + y * data.Stride; 43 byte pos = (byte)(7 - x % 8); 44 var val = (byte)((p[0] & ((byte)0x01 << pos)) >> pos); 45 pDest[0] = val == 1 ? (byte) 255 : (byte) 0; 46 } 47 } 48 } 49 50 bmpDest.UnlockBits(dataDest); 51 bmp.UnlockBits(data); 52 53 bmpDest.Save("result8.bmp"); 54 55 Process.Start("result8.bmp"); 56 } 57 58 private void button2_Click(object sender, EventArgs e) 59 { 60 var bmp = Image.FromFile("test24.png") as Bitmap; 61 62 var w = bmp.Width; 63 var h = bmp.Height; 64 65 var bmpDest = new Bitmap(w, h, PixelFormat.Format1bppIndexed); 66 67 //bmpDest.SetResolution(bmp.HorizontalResolution, bmpDest.VerticalResolution); 68 69 var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, 70 PixelFormat.Format24bppRgb); 71 72 var dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, 73 PixelFormat.Format1bppIndexed); 74 unsafe 75 { 76 for (int x = 0; x < w; x++) 77 { 78 for (int y = 0; y < h; y++) 79 { 80 byte* p = (byte*)data.Scan0 + x * 3 + y * data.Stride; 81 byte* pDest = (byte*)dataDest.Scan0 + x / 8 + y * dataDest.Stride; 82 var bMask = (byte) (0x0080 >> (int) (x%8)); 83 if (p[2] > 128) 84 *pDest |= bMask; 85 else 86 *pDest &= (byte) ~bMask; 87 } 88 } 89 } 90 91 bmpDest.UnlockBits(dataDest); 92 bmp.UnlockBits(data); 93 bmp.Dispose(); 94 bmpDest.Save("result1.tif"); 95 bmpDest.Dispose(); 96 Process.Start("result1.tif"); 97 } 98 99 private void button4_Click(object sender, EventArgs e) 100 { 101 var img = Image.FromFile("test24.png") as Bitmap; 102 103 var bit = new Bitmap(img.Width, img.Height, PixelFormat.Format8bppIndexed); 104 var data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); 105 var data2 = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); 106 unsafe 107 { 108 for (int i = 0; i != data.Height; i++) 109 { 110 for (int j = 0; j != data.Width; j++) 111 { 112 byte* p = (byte*) data.Scan0 + j*3 + i*data.Stride; 113 byte* pDest = (byte*)data2.Scan0 + j + i * data2.Stride; 114 //0.3R+0.59G+0.11B 115 float value = 0.11F * p[0] + 0.59F *p[1] + 0.3F * p[2]; 116 pDest[0] = (byte)value; 117 } 118 } 119 } 120 121 img.UnlockBits(data); 122 bit.UnlockBits(data2); 123 img.Dispose(); 124 125 ColorPalette palette = bit.Palette; 126 for (int i = 0; i != palette.Entries.Length; i++) 127 { 128 palette.Entries[i] = Color.FromArgb(i, i, i); 129 } 130 bit.Palette = palette; 131 132 bit.Save("result8.bmp"); 133 bit.Dispose(); 134 Process.Start("result8.bmp"); 135 } 136 137 private void button3_Click(object sender, EventArgs e) 138 { 139 var bmp = Image.FromFile("test8.bmp") as Bitmap; 140 141 var w = bmp.Width; 142 var h = bmp.Height; 143 144 var bmpDest = new Bitmap(w, h, PixelFormat.Format1bppIndexed); 145 146 bmpDest.SetResolution(bmp.HorizontalResolution, bmpDest.VerticalResolution); 147 148 var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, 149 PixelFormat.Format8bppIndexed); 150 151 var dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, 152 PixelFormat.Format1bppIndexed); 153 unsafe 154 { 155 for (int x = 0; x < w; x++) 156 { 157 for (int y = 0; y < h; y++) 158 { 159 byte* p = (byte*)data.Scan0 + x + y * data.Stride; 160 byte* pDest = (byte*)dataDest.Scan0 + x / 8 + y * dataDest.Stride; 161 var bMask = (byte)(0x0080 >> (int)(x % 8)); 162 if (p[2] > 128) 163 *pDest |= bMask; 164 else 165 *pDest &= (byte)~bMask; 166 } 167 } 168 } 169 170 bmpDest.UnlockBits(dataDest); 171 bmp.UnlockBits(data); 172 173 bmp.Dispose(); 174 175 bmpDest.Save("result1.tif"); 176 bmpDest.Dispose(); 177 Process.Start("result1.tif"); 178 } 179 180 private void button5_Click(object sender, EventArgs e) 181 { 182 var img = Image.FromFile("test8.bmp") as Bitmap; 183 184 var bit = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb); 185 var data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed); 186 var data2 = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); 187 unsafe 188 { 189 for (int i = 0; i != data.Height; i++) 190 { 191 for (int j = 0; j != data.Width; j++) 192 { 193 byte* p = (byte*)data.Scan0 + j + i * data.Stride; 194 byte* pDest = (byte*)data2.Scan0 + j * 3 + i * data2.Stride; 195 196 if(p[0] < 128) 197 { 198 pDest[0] = 0; 199 pDest[1] = 0; 200 pDest[2] = 0; 201 } 202 else 203 { 204 pDest[0] = 255; 205 pDest[1] = 255; 206 pDest[2] = 255; 207 } 208 } 209 } 210 } 211 212 img.UnlockBits(data); 213 bit.UnlockBits(data2); 214 img.Dispose(); 215 216 bit.Save("result24.png"); 217 bit.Dispose(); 218 Process.Start("result24.png"); 219 } 220 221 private void button6_Click(object sender, EventArgs e) 222 { 223 224 } 225 226 private void button8_Click(object sender, EventArgs e) 227 { 228 var img = Image.FromFile("test8.bmp") as Bitmap; 229 var data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed); 230 231 var imgDest = new Bitmap(460, 350, PixelFormat.Format8bppIndexed); 232 233 var dataDest = imgDest.LockBits(new Rectangle(0, 0, imgDest.Width, imgDest.Height), ImageLockMode.ReadWrite, 234 PixelFormat.Format8bppIndexed); 235 236 int x, y; 237 double sx, sy; 238 double u, v; 239 double[] pm = new double[4]; 240 byte[] color = new byte[4]; 241 242 double w = (double) img.Width/(double) imgDest.Width; 243 double h = (double) img.Height/(double) imgDest.Height; 244 245 unsafe 246 { 247 for (int row = 0; row < imgDest.Height; row++) 248 { 249 sy = (double) (row + 0.5)*h - 0.5; 250 y = (int) sy; 251 if (y > sy) 252 y--; 253 254 v = sy - y; 255 256 for (int col = 0; col < imgDest.Width; col++) 257 { 258 sx = (double) (col + 0.5)*w - 0.5; 259 x = (int) sx; 260 if (x > sx) 261 x--; 262 263 u = sx - x; 264 265 pm[0] = (1.0 - u)*(1.0 - v); 266 pm[1] = v*(1.0 - u); 267 pm[2] = u*(1.0 - v); 268 pm[3] = u*v; 269 270 for (int n = 0; n < 4; n++) 271 { 272 int xx = (n == 0 || n == 1) ? x : x + 1; 273 if (xx < 0) 274 xx = 0; 275 else if (xx > img.Width - 1) 276 xx = img.Width - 1; 277 278 int yy = (n == 0 || n == 2) ? y : y + 1; 279 if (yy < 0) 280 yy = 0; 281 else if (yy > img.Height - 1) 282 yy = img.Height - 1; 283 284 color[n] = ((byte*)data.Scan0 + xx + yy*data.Stride)[0]; 285 } 286 287 byte val = (byte) (pm[0]*color[0] + pm[1]*color[1] + pm[2]*color[2] + pm[3]*color[3]); 288 289 byte* p = ((byte*)dataDest.Scan0 + col + row * dataDest.Stride); 290 *p = val; 291 } 292 } 293 } 294 295 imgDest.UnlockBits(dataDest); 296 img.UnlockBits(data); 297 img.Dispose(); 298 299 ColorPalette palette = imgDest.Palette; 300 for (int i = 0; i != palette.Entries.Length; i++) 301 { 302 palette.Entries[i] = Color.FromArgb(i, i, i); 303 } 304 imgDest.Palette = palette; 305 306 imgDest.Save("scaled.bmp"); 307 308 imgDest.Dispose(); 309 310 Process.Start("scaled.bmp"); 311 } 312 313 private void button7_Click(object sender, EventArgs e) 314 { 315 var bmp = Image.FromFile("test1.tif") as Bitmap; 316 317 var w = bmp.Width; 318 var h = bmp.Height; 319 320 var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, 321 PixelFormat.Format1bppIndexed); 322 unsafe 323 { 324 for (int x = 0; x < w; x++) 325 { 326 for (int y = 0; y < h; y++) 327 { 328 byte* p = (byte*)data.Scan0 + x / 8 + y * data.Stride; 329 330 if(*p == 0) 331 { 332 333 } 334 } 335 } 336 } 337 338 bmp.UnlockBits(data); 339 340 bmp.Save("result.tif"); 341 342 Process.Start("result.tif"); 343 } 344 345 private void button7_Click_1(object sender, EventArgs e) 346 { 347 var bmp = Image.FromFile("test1.tif") as Bitmap; 348 bmp.Save("test.bmp", ImageFormat.Jpeg); 349 } 350 } 351 }