.NET 1,8,24位图片转换

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;
    
 namespace TypeConverter
 {
     using System.Diagnostics;
     using System.Drawing.Imaging;
    
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             var bmp = Image.FromFile("test1.tif") as Bitmap;
    
             var w = bmp.Width;
             var h = bmp.Height;
    
             var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
                                     PixelFormat.Format1bppIndexed);
    
             var bmpDest = new Bitmap(w, h, PixelFormat.Format8bppIndexed);
             var dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
                                     PixelFormat.Format8bppIndexed);
             unsafe
             {
                 for (int y = 0; y < h; y++)
                 {
                     for (int x = 0; x < w; x++)
                     {
                         byte* pDest = (byte*)dataDest.Scan0 + x  + y * dataDest.Stride;
                         byte* p = (byte*)data.Scan0 + x/8 + y * data.Stride;
                         byte pos = (byte)(7 - x % 8);
                         var val = (byte)((p[0] & ((byte)0x01 << pos)) >> pos);
                         pDest[0] = val == 1 ? (byte) 255 : (byte) 0;
                     }
                 }
             }
    
             bmpDest.UnlockBits(dataDest);
             bmp.UnlockBits(data);
    
             bmpDest.Save("result8.bmp");
    
             Process.Start("result8.bmp");
         }
    
         private void button2_Click(object sender, EventArgs e)
         {
             var bmp = Image.FromFile("test24.png") as Bitmap;
    
             var w = bmp.Width;
             var h = bmp.Height;
    
             var bmpDest = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
    
             //bmpDest.SetResolution(bmp.HorizontalResolution, bmpDest.VerticalResolution);
    
             var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
                                     PixelFormat.Format24bppRgb);
    
             var dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
                                     PixelFormat.Format1bppIndexed);
             unsafe
             {
                 for (int x = 0; x < w; x++)
                 {
                     for (int y = 0; y < h; y++)
                     {
                         byte* p = (byte*)data.Scan0 + x * 3 + y * data.Stride;
                         byte* pDest = (byte*)dataDest.Scan0 + x / 8 + y * dataDest.Stride;
                         var bMask = (byte) (0x0080 >> (int) (x%8));
                         if (p[2] > 128)
                             *pDest |= bMask;
                         else
                             *pDest &= (byte) ~bMask;
                     }
                 }
             }
    
             bmpDest.UnlockBits(dataDest);
             bmp.UnlockBits(data);
             bmp.Dispose();
             bmpDest.Save("result1.tif");
             bmpDest.Dispose();
             Process.Start("result1.tif");
         }
    
         private void button4_Click(object sender, EventArgs e)
         {
             var img = Image.FromFile("test24.png") as Bitmap;
    
             var bit = new Bitmap(img.Width, img.Height, PixelFormat.Format8bppIndexed);
             var data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
             var data2 = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
             unsafe
             {
                 for (int i = 0; i != data.Height; i++)
                 {
                     for (int j = 0; j != data.Width; j++)
                     {
                         byte* p = (byte*) data.Scan0 + j*3 + i*data.Stride;
                         byte* pDest = (byte*)data2.Scan0 + j + i * data2.Stride;
                         //0.3R+0.59G+0.11B
                         float value = 0.11F * p[0] + 0.59F *p[1] + 0.3F * p[2];
                         pDest[0] = (byte)value;
                     }
                 }
             }
    
             img.UnlockBits(data);
             bit.UnlockBits(data2);
             img.Dispose();
    
             ColorPalette palette = bit.Palette;
             for (int i = 0; i != palette.Entries.Length; i++)
             {
                 palette.Entries[i] = Color.FromArgb(i, i, i);
             }
             bit.Palette = palette;
    
             bit.Save("result8.bmp");
             bit.Dispose();
             Process.Start("result8.bmp");
         }
    
         private void button3_Click(object sender, EventArgs e)
         {
             var bmp = Image.FromFile("test8.bmp") as Bitmap;
    
             var w = bmp.Width;
             var h = bmp.Height;
    
             var bmpDest = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
    
             bmpDest.SetResolution(bmp.HorizontalResolution, bmpDest.VerticalResolution);
    
             var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
                                     PixelFormat.Format8bppIndexed);
    
             var dataDest = bmpDest.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
                                     PixelFormat.Format1bppIndexed);
             unsafe
             {
                 for (int x = 0; x < w; x++)
                 {
                     for (int y = 0; y < h; y++)
                     {
                         byte* p = (byte*)data.Scan0 + x + y * data.Stride;
                         byte* pDest = (byte*)dataDest.Scan0 + x / 8 + y * dataDest.Stride;
                         var bMask = (byte)(0x0080 >> (int)(x % 8));
                         if (p[2] > 128)
                             *pDest |= bMask;
                         else
                             *pDest &= (byte)~bMask;
                     }
                 }
             }
    
             bmpDest.UnlockBits(dataDest);
             bmp.UnlockBits(data);
    
             bmp.Dispose();
    
             bmpDest.Save("result1.tif");
             bmpDest.Dispose();
             Process.Start("result1.tif");
         }
    
         private void button5_Click(object sender, EventArgs e)
         {
             var img = Image.FromFile("test8.bmp") as Bitmap;
    
             var bit = new Bitmap(img.Width, img.Height, PixelFormat.Format24bppRgb);
             var data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
             var data2 = bit.LockBits(new Rectangle(0, 0, bit.Width, bit.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
             unsafe
             {
                 for (int i = 0; i != data.Height; i++)
                 {
                     for (int j = 0; j != data.Width; j++)
                     {
                         byte* p = (byte*)data.Scan0 + j + i * data.Stride;
                         byte* pDest = (byte*)data2.Scan0 + j * 3 + i * data2.Stride;
                            
                         if(p[0] < 128)
                         {
                             pDest[0] = 0;
                             pDest[1] = 0;
                             pDest[2] = 0;
                         }
                         else
                         {
                             pDest[0] = 255;
                             pDest[1] = 255;
                             pDest[2] = 255;
                         }
                     }
                 }
             }
    
             img.UnlockBits(data);
             bit.UnlockBits(data2);
             img.Dispose();
    
             bit.Save("result24.png");
             bit.Dispose();
             Process.Start("result24.png");
         }
    
         private void button6_Click(object sender, EventArgs e)
         {
    
         }
    
         private void button8_Click(object sender, EventArgs e)
         {
             var img = Image.FromFile("test8.bmp") as Bitmap;
             var data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
    
             var imgDest = new Bitmap(460, 350, PixelFormat.Format8bppIndexed);
    
             var dataDest = imgDest.LockBits(new Rectangle(0, 0, imgDest.Width, imgDest.Height), ImageLockMode.ReadWrite,
                                             PixelFormat.Format8bppIndexed);
    
             int x, y;
             double sx, sy;
             double u, v;
             double[] pm = new double[4];
             byte[] color = new byte[4];
    
             double w = (double) img.Width/(double) imgDest.Width;
             double h = (double) img.Height/(double) imgDest.Height;
    
             unsafe
             {
                 for (int row = 0; row < imgDest.Height; row++)
                 {
                     sy = (double) (row + 0.5)*h - 0.5;
                     y = (int) sy;
                     if (y > sy)
                         y--;
    
                     v = sy - y;
    
                     for (int col = 0; col < imgDest.Width; col++)
                     {
                         sx = (double) (col + 0.5)*w - 0.5;
                         x = (int) sx;
                         if (x > sx)
                             x--;
    
                         u = sx - x;
    
                         pm[0] = (1.0 - u)*(1.0 - v);
                         pm[1] = v*(1.0 - u);
                         pm[2] = u*(1.0 - v);
                         pm[3] = u*v;
    
                         for (int n = 0; n < 4; n++)
                         {
                             int xx = (n == 0 || n == 1) ? x : x + 1;
                             if (xx < 0)
                                 xx = 0;
                             else if (xx > img.Width - 1)
                                 xx = img.Width - 1;
    
                             int yy = (n == 0 || n == 2) ? y : y + 1;
                             if (yy < 0)
                                 yy = 0;
                             else if (yy > img.Height - 1)
                                 yy = img.Height - 1;
    
                             color[n] = ((byte*)data.Scan0 + xx + yy*data.Stride)[0];
                         }
    
                         byte val = (byte) (pm[0]*color[0] + pm[1]*color[1] + pm[2]*color[2] + pm[3]*color[3]);
    
                         byte* p = ((byte*)dataDest.Scan0 + col + row * dataDest.Stride);
                         *p = val;
                     }
                 }
             }
                
             imgDest.UnlockBits(dataDest);
             img.UnlockBits(data);
             img.Dispose();
    
             ColorPalette palette = imgDest.Palette;
             for (int i = 0; i != palette.Entries.Length; i++)
             {
                 palette.Entries[i] = Color.FromArgb(i, i, i);
             }
             imgDest.Palette = palette;
    
             imgDest.Save("scaled.bmp");
    
             imgDest.Dispose();
    
             Process.Start("scaled.bmp");
         }
    
         private void button7_Click(object sender, EventArgs e)
         {
             var bmp = Image.FromFile("test1.tif") as Bitmap;
    
             var w = bmp.Width;
             var h = bmp.Height;
    
             var data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite,
                                     PixelFormat.Format1bppIndexed);
             unsafe
             {
                 for (int x = 0; x < w; x++)
                 {
                     for (int y = 0; y < h; y++)
                     {
                         byte* p = (byte*)data.Scan0 + x / 8 + y * data.Stride;
    
                         if(*p == 0)
                         {
                                
                         }
                     }
                 }
             }
    
             bmp.UnlockBits(data);
    
             bmp.Save("result.tif");
    
             Process.Start("result.tif");
         }
    
         private void button7_Click_1(object sender, EventArgs e)
         {
             var bmp = Image.FromFile("test1.tif") as Bitmap;
             bmp.Save("test.bmp", ImageFormat.Jpeg);
         }
     }
 }


知识共享许可协议
《.NET 1,8,24位图片转换》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     180.06ms