当前位置:文档之家› 8、24、32位图位数的相互转换

8、24、32位图位数的相互转换


利达光电股份有限公司 | 彭军
9
2010 年 4 月 23 日
位图位数的转换
namespace BitmapConverter { class NeverBitmap { struct BitmapFileHeader {//位图文件头 public UInt16 bfType; public UInt32 bfSize; public UInt16 bfReserved1; public UInt16 bfReserved2; public UInt32 bfOffBits; } struct BitmapInfoHeader {//位图信息头 public UInt32 biSize; public UInt32 biWidth; public UInt32 biHeight; public UInt16 biPlanes; public UInt16 biBitCount; public UInt32 biCompression; public UInt32 biSizeImage; public UInt32 biXPelsPerMeter; public UInt32 biYPelsPerMeter; public UInt32 biClrUsed; public UInt32 biClrImportant; } struct RGBQUAD {//位图调色板项 public byte rgbBlue; public byte rgbGreen; public byte rgbRed; public byte rgbReserved; }
利达光电股份有限公司 | 彭军 5
2010 年 4 月 23 日
位图位数的转换
return; } if (tbxDirectory.Text.Trim() == tbxSaveDir.Text.Trim()) { MessageBox.Show("两个目录不能相同!", "错误"); return; } progressBar1.Value = 0; progressBar1.Minimum = 0; progressBar1.Maximum = files.Length; for (int i = 0; i < files.Length; i++) { ListViewItem lvi = listView1.Items[i]; UInt16 choose = Convert.ToUInt16(cbxBitCount.Text.Trim().ToString()); string savepath=tbxSaveDir.Text.Trim() +"\\"+ GetFileName(lvi.SubItems[0].Text.Trim()); NeverBitmap nb = new NeverBitmap(lvi.SubItems[0].Text.Trim(),savepath); if (lvi.SubItems[1].Text == cbxBitCount.Text) { progressBar1.Value++; lvi.SubItems[0].Text += " 无需转换"; continue; } else if (lvi.SubItems[1].Text == "8") { switch (choose) { case 24: nb.Bit8To24(); break;
利达光电股份有限公司
位图位数的转换
用 VC#2008 实现
彭军
2010
可以实现: 8 位到 24 位、8 位到 32 位的转换 24 位到 8 位、24 位到 32 位的转换 32 位到 8n@
河 南 省 南阳 市 工业 南路 50 8 号
2010 年 4 月 23 日
位图位数的转换利达光电股份有限公 Nhomakorabea | 彭军
2
2010 年 4 月 23 日
位图位数的转换
Form1.cs 的全部程序: using System; using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace BitmapConverter { public partial class Form1 : Form { FileInfo[] files = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
利达光电股份有限公司 | 彭军 7
2010 年 4 月 23 日
位图位数的转换
lvi.SubItems[0].Text += " 完成"; } } private void tbxSaveDir_Click(object sender, EventArgs e) { //设置保存目录,不能和原目录一致 FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() != DialogResult.OK) return; tbxSaveDir.Text = fbd.SelectedPath; } } } BitmapInfo.cs 的全部程序: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Imaging; namespace BitmapConverter { class BitmapInfo { Bitmap bmp = null; public BitmapInfo(string path) { bmp = new Bitmap(path); } public int GetWidth()
利达光电股份有限公司 | 彭军 8
2010 年 4 月 23 日
位图位数的转换
{//返回位图宽度 if (bmp==null) return -1; return bmp.Width; } public int GetHeight() {//返回位图高度 if (bmp==null) return -1; return bmp.Height; } public int GetBitCount() {//返回位图位数 if (bmp.PixelFormat == PixelFormat.Format24bppRgb) return 24; else if (bmp.PixelFormat == PixelFormat.Format32bppRgb || bmp.PixelFormat == PixelFormat.Format32bppPArgb || bmp.PixelFormat == PixelFormat.Format32bppArgb) return 32; else if (bmp.PixelFormat == PixelFormat.Format8bppIndexed) return 8; else return -1; } } } NeverBitmap.cs 的全部程序: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing.Imaging; using System.Drawing; using System.IO;
利达光电股份有限公司 | 彭军 3
2010 年 4 月 23 日
位图位数的转换
{ //设置窗口的大小不可变 this.MinimumSize = this.Size; this.MaximumSize = this.Size; //设置默认转换为24位位图 cbxBitCount.SelectedIndex = 1; } //列出当前目录下位图文件 private void ListBmpFiles() { try { DirectoryInfo di = new DirectoryInfo(tbxDirectory.Text.Trim()); files = di.GetFiles("*.bmp"); foreach (FileInfo f in files) { ListViewItem lvi = listView1.Items.Add(f.FullName); BitmapInfo bi = new BitmapInfo(f.FullName); lvi.SubItems.Add(bi.GetBitCount().ToString()); lvi.SubItems.Add(bi.GetHeight().ToString()); lvi.SubItems.Add(bi.GetWidth().ToString()); } } catch (Exception e) { MessageBox.Show(e.Message, @"错误"); return; } } private void tbxDirectory_Click(object sender, EventArgs e) { //选择需转换的位图文件所在的目录
利达光电股份有限公司 | 彭军 4
2010 年 4 月 23 日
位图位数的转换
FolderBrowserDialog fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() != DialogResult.OK) return; tbxDirectory.Text = fbd.SelectedPath; listView1.Items.Clear();//清空ListView中的内容 progressBar1.Value = 0;//还原进度条 ListBmpFiles(); } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } //从路径中分割出文件名 private string GetFileName(string path) { string[] parts = path.Split('\\'); return parts[parts.Length - 1]; } private void btnStart_Click(object sender, EventArgs e) { if (cbxBitCount.Text.Trim() == "") { MessageBox.Show("请选择转换后的位图位数!", "错误"); return; } if (tbxDirectory.Text.Trim() == "") { MessageBox.Show("请选择位图所在的目录!", "错误"); return; } if (files == null || files.Length == 0) { MessageBox.Show("当前目录下没有位图!", "错误");
相关主题