




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、利達光電股份有限公司位圖位數的轉換用VC#2008實現彭軍河南省南陽市工業南路508號2010年4月23曰利達光電股份有限公司|彭軍32010年4月23曰吟Bitmapconverter【口I迭擇WIPES片目錄:E照片樣品囲片轉換后為:32位保存目錄:E:開瞬換|退出程序|彭軍出品老屬精品利達光電股份有限公司|彭軍#2010年4月23曰SolutionExplorer-BitmapConverter¥X隠l滲屋丨國打Solution'BitmapConverter1(1project)S-國BitmapConverter旦Properties!型References曹Bit
2、maplnfo.es白匡|Forml.cs也Forml.Designer.es崔|Forml.resx崔|NeverBitmap.cs曹Program.csForml.cs的全部程序:usingSystem;usingSystentCollectionsGeneric;usingSystern.ComponentModel:usingSystentData;usingSystentDrawing;usingSysteniLinq;usingSystentText;usingSystentWindows.Forms;usingSystentIO;namespaceBitmapConverterp
3、ublicpartialclassForml:FormFileinfofiles=null:publicForml()InitializeComponent();privatevoidForm1_Load(objectsender,EventArgse)/設置窗口的大小不可變this.MinimumSize=this.Size;this.MaximumSize=this.Size;設置默認轉換為24位位圖cbxBitCount.Selectedlndex=1:/列出當前目錄下位圖文件privatevoidListBnFilesOtryDirectorylnfodi=newDirectoryln
4、fo(tbxDirectory.TextTrimO);files=di.GetFiles("水.bmp");foreach(Fileinfofinfiles)ListViewItemlvi=listViewl.ItemsAdd(fFullName);Bitmapinfobi=newBitmapInfo(f.FullName):lvi.SubItemsAdd(biGetBitCount()ToStringO);lvi.SubItemsAdd(biGetHeight()ToString();lvi.SubItems.Add(bi.GetWidth().ToStringO);ca
5、tch(Exceptione)MessageBox.Show(e.Message,"錯誤");return;privatevoidtbxDirectory_Click(objectsender,EventArgse)/選擇需轉換的位圖文件所在的目錄FolderBrowserDialogfbd=newFolderBrowserDialog();if(fbd.ShoWialogO!=DialogResult.OK)return;tbxDirectory.Text二fbd.SelectedPath;1istViewl.Items.Clear();清空ListView中的內容pro
6、gressBarl.Value=0;/還原進度條ListBupFilesO;privatevoidbtnExitJC1ick(objectsender,EventArgse)this.Close();/從路徑中分割出文件名privatestringGetFileName(stringpath)stringparts=path.Split('');returnpartsparts.Length-1:privatevoidbtnStartjClick(objectsender,EventArgse)if(cbxBitCount.Text.Trim()="”)Message
7、Box.Show(”請選擇轉換后的位圖位數!","錯誤”);return;if(tbxDirectory.Text.Trim()="”)MessageBox.Show(”請選擇位圖所在的LI錄!","錯誤");return;if(files=null|files.Length=0)MessageBox.Show(”當前目錄下沒有位圖!","錯誤");return;if(tbxDirectory.TextTrim()=tbxSaveDirTextTrim()MessageBox.ShowC兩個H錄不能相同!
8、”,"錯誤”);return;progressBar1.Value=0;progressBar1.Minimum=0;progressBar1.Maximum=files.Length;for(inti二0;ifiles.Length;i+)ListViewItemlvi=listViewl.ltemsi;UIntl6choose=ConvertToUIntl6(cbxBitCountTextTrim()ToString();stringsavepath二tbxSaveDir.TextTrim()+""+GetFileName(lvi.SubItems0TextT
9、rim();NeverBitmapnb=newNeverBitmap(lvi.SubItems0TextTrim(),savepath);if(lvi.SubItemslText=cbxBitCount.Text)progressBar1.Value+;lvi.Sublterns0.Text+二"無需轉換";continue;elseif(lvi.SubItems1.Text="8")switch(choose)case24:nb.Bit8To24();break;case32:nb.Bit8To32();break;elseif(lvi.Subitem
10、s1.Text="24")switch(choose)case8:nb.Bit24To8();break;case32:nb.Bit24To32();break;elseif(lvi.Subitems1.Text="32")switch(choose)case8:nb.Bit32To8();break;case24:nb.Bit32To24();break;elsecontinue;progressBar1.Value+;lvi.Subitems0.Text+="完成";privatevoidtbxSaveDir_Click(obje
11、ctsender,EventArgse)設置保存目錄,不能和原目錄一致FolderBrowserDialogfbd=newFolderBrowserDialog();if(fbd.Shovi)ialog()!=DialogResult0K)return;tbxSaveDirText二fbd.SelectedPsth;Bitmaplnfo.es的全部程序:usingSystem;usingSystentCollectionsGeneric;usingSystentLinq:usingSystem.Text;usingSystem.10;usingSystentDrawing;usingSyste
12、ntDrawing.Imaging;namespaceBitmapConverterclassBitmapInfoBitmapbmp=null;publicBitmaplnfo(stringpath)bw=newBitmap(path);publicintGetWidthO/返回位圖寬度if(bnp=null)returnT;returnbmp.Width;publicintGetHeight()/返回位圖高度if(bnp=null)returnT;returnbmp.Height;publicintGetBitCount()/返回位圖位數if(bnp.PixelFormat=PixelFor
13、mat.Format24bppRgb)return24;elseif(bmp.PixelFormat=PixelFormat.Format32bppRgb|bnp.PixelFormat=PixelFormat.Format32bppPArgb|bnp.PixelFormat=PixelFormat.Format32bppArgb)return32;elseif(bmp.PixelFormat=PixelFormat.Format8bpplndexed)return8;elsereturnT;NeverBitmap.es的全部程序:usingSystem;usingSystentCollect
14、ionsGeneric;usingSysteniLinq;usingSystentText;usingSystentDrawing.Imaging;usingSystentDrawing;usingSystent10;利達光電股份有限公司|彭軍112010年4月23曰利達光電股份有限公司|彭軍#2010年4月23曰namespaceBitmapConverterclassNeverBitmapstructBitmapFileHeader/位圖文件頭publicUIntl6publicUInt32bfType;bfSize:publicUIntl6publicpublicUIntl6UInt32
15、bfReservedl;bfReserved2;bfOffBits;structBitmapInfoHeader/位圖信息頭publicUInt32biSize:publicUInt32biWidth;publicUInt32biHeight;publicUIntl6biPlanes:publicUIntl6biBitCount;publicUInt32biCompression;publicUInt32biSizelmage;publicUInt32biXPelsPerMeter;publicUInt32biYPelsPerMeter;publicUInt32biClrUsed;public
16、UInt32biClrlnportant;structRGBQUAD/位圖調色板項publicbytergbBlue;publicbytergbGreen:publicbytergbRed:publicbytergbReserved;利達光電股份有限公司|彭軍132010年4月23曰privatestringf訂epath=null;/需打開的位圖的路徑privatestringsavepath二null;/轉換后位圖的保存路徑privateBitmapFi1eHeaderbfh;privateBitmapInfoHeaderbih:publicNeverBitmap(stringopenpa
17、th,stringsavepath)thisfilepath=openDath;this,savepath=savepath;privatevoidSaveSBits(Bitmapbmp)/為位圖文件頭賦值bfh.bfOffBits=107&bfh.bfReservedl=0;bfh.bfReserved2=0;bfh.bfSize=14;bfh.bfType=19778;為位圖信息頭賦值bihbiBitCount二8:bih.biClrlnportant=0;bih.biClrUsed=0;bih.biCompression=0:bih.biHeight=(uint)bn.Heigh
18、t;bih.biPlanes=1;bih.biSize=40;bih.biSizeImage=(uint)(bnp.Height*(bmp.Width*8+31)/32*4);bih.biWidth二(uint)bmp.Width:bih.biXPelsPerMeter二0;bih.biYPelsPerMeter=0;/構造256色的調色板,非索引圖R(SQUADpal二newRGBQUAD256;for(inti二0;i256;i卄)palirgbBlue=(byte)i;palirgbGreen=(byte)i;palirgbRed=(byte)i;palirgbReserved=0;Fi
19、lelnfof=newFileinfo(savepath);using(BinaryV/riterbw=newBinaryWriter(f.OpenWrite()寫入文件頭bw.Vftrite(bfhbfType);bw.Write(bfh.bfSize);bw.Vftrite(bfhbfReservedl);bw.Vftrite(bfhbfReserved2);bw.Write(bfh.bfOffBits);寫入信息頭bw.Write(bih.biSize);bw.Write(bih.biWidth);bw.Write(bih.biHeight);bw.Vftrite(bih.biPlane
20、s);bw.毗ite(bihbiBitCount);bw.毗ite(bih.biCompression);bw.毗ite(bihbiSizeimage);bw.Write(bih.biXPelsPerMeter);bw.Write(bih.biYPelsPerMeter);bw.Write(bih.biClrUsed);bw.Vftrite(bih.biClrImportant);寫入調色板for(inti=0;i<256;i+)bw.Write(palirgbBlue);bw.V/rite(palirgbGreen);bw.V/rite(palirgbRed);bw.Write(pal
21、irgbReserved);/位圖上下翻轉bnDRototeFlip(RototeFlipTypeRototel80FlipX);BitmapDatsdata=bmp.LockBits(newRectangle©0,bnpWidt扎bmp.Height),ImageLockMode.Readonly,PixelFormat.Format8bppIndexed);unsafebyte*ptr=(byte*)data.ScanO.ToPointer()J/L圖的指針byteline=newbytedata.Stride:/保存位圖的一行for(inti=0;i<data.Strid
22、e:i+)linei=0;for(inti=0;i<bnp.Height;i+)for(intj二0;jbmp.Width;j+)linefj=*ptr+;ptr+二data.Stride-bn?).Width;/Jr針跳過對齊的字節bw.Write(line,0,line.Length):寫入位圖的一行bw.Close();bnpUnlockBits(dato);publicvoidBit8To24()Bitmapbmp8=newBitmap(filepath);BitmapDatsdata8=bmp8LockBits(newRectangle(0,0,bmp8Width,bnp8.H
23、eight),ImageLockModeReadonly,PixelFormatFormat8bppIndexed);Bitmapbmp24=newBitmap(bmp8.Width,bnp8.Height,PixelFormat.Format24bppRgb);BitmapDatsdata24=bmp24.LockBits(newRectangle©0,利達光電股份有限公司|彭軍172010年4月23曰位圖彳盤的超bnp24.Width,bn?)24.Height),ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);unsafe
24、byte*ptr8=(byte*)data8.ScanO.ToPointer();byte*ptr24=(byte*)data24.ScanO.ToPointerO;for(inti=0;i<bropS.Height;i+)for(intj=0;j<b叩8.Width;j+)/用8位位圖的灰度值填充24位位圖的R、G、B值*ptr24+二切tr8;*ptr24+二切tr8;*ptr24+二切tr8+;ptr8+二data8.Stride-bn?)8.Width;/跳過對齊字節ptr24+二data24.Stride-bn?)8.Width*3;/跳過對齊字節bw8UnlockBit
25、s(data8);bw24.UnlockBits(data24);bw24.Save(savepath);publicvoidBit8To32()Bitmapbmp8=newBitmap(filepath);BitmapDatadata8=bnp8LockBits(newRectangle(0,0,bmp8Width,bnp8.Height),ImageLockModeReadonly,PixelFormatFormat8bppIndexed);Bitmapbmp32=newBitmap(bmp8.Width,bnp8.Height,PixelFormat.Format32bppArgb);B
26、itmapDatadata32=bmp32.LockBits(newRectangle©0,bnp32.Width,bn?)32Height),ImageLockModeWriteOnly,PixelFormat.Format32bppArgb);利達光電股份有限公司|彭軍192010年4月23曰unsafebyte*ptr8=(byte*)data8Scan0.ToPointer();byte*ptr32=(byte*)data32.ScanO.ToPointerO;for(inti=0;i<bn)8.Height;i+)for(intj=0;j<b叩&Widt
27、h:j+)/用8位位圖的灰度值,填充32位位圖的RGB值,透明度為100%*ptr32+二切比8;*ptr32+二切比8;*ptr32+=切tr8+;*ptr32+=255;ptr8udata8Stride一bn?)8Width;ptr32+二data32.Stride-bmp8.Width*4;bw8UnlockBits(data8);bw32UnlockBits(dats32);bw32Save(savepath);publicvoidBit24To8()Bitmapbmp24=newBitmap(filepath);BitmapDatsdata24=bmp24LockBits(newRe
28、ctangle©0,bnp24.Width,bn?)24.Height),ImageLockModeReadonly,PixelFormat.Format24bppRgb);Bitmapbmp8=newBitmap(bnp24.Width,bmp24.Height,PixelFormat.Format8bppIndexed);BitmapDatsdata8=bmp8LockBits(newRectangle(0,0,bmp8.Width,bn8.Height),ImageLockModeWriteOnly,PixelFormatFormat8bppIndexed);unsafebyt
29、e*ptr24=(byte*)data24.ScanO.ToPointer();byte*ptr8=(byte*)data8.ScanO.ToPointer();for(inti=0;i<brop8.Height;i+)for(intj=0;j<bw8.Width:j+)/用RGB值的均值作為8位位圖的灰度值*ptr8+=(byte)(int)(*ptr24+)+(int)(>:<ptr24+)+(int)(切tr24卄)/3);ptr24+二data24.Stride-bmp8.Width*3;ptr8udata8.Stride一bn?8.Width;bw8Unloc
30、kBits(data8);bw24.UnlockBits(data24);Save8Bits(bmp8);publicvoidBit32To8()Bitmapbmp32=newBitmap(filepath);BitmapDatodata32=bmp32.LockBits(newRectangle©0,bnp32.Width,bn>32Height),ImageLockModeReadonly,PixelFormatFormat32bppArgb);Bitmapbmp8=newBitmap(bnp32Width,bmp32.Height,PixelFormatFormat8bp
31、pIndexed);BitmapDatodata8=bnp8LockBits(newRectangle(0,0,bmp8.Width,bnp8.Height),ImageLockModeWriteOnly,PixelFormatFormat8bppIndexed);unsafebyte*ptr32=(byte*)data32.ScanO.ToPointer();byte*ptr8=(byte*)data8Scan0ToPointer();for(inti=0;i<bropS.Height;i+)for(intj=0;j<bw8.Width:j+)/用32位位圖的RC®值的
32、均值作為8位位圖的灰度值*ptr8+二(byte)(int)Gptr32卄)+(int)(即比32+)+(int)(*ptr32+)/3);ptr32卄;跳過透明度字節ptr32+=data32.Stride-bmp32Width*4;ptr8udata8Stride一bn?)8.Width;bw8UnlockBits(data8);bw32UnlockBits(dato32);Save8Bits(bmp8);publicvoidBit32To24()Bitmapbmp32=newBitmap(filepath);BitmapDatodata32=bmp32.LockBits(newRecta
33、ngle©0,bnp32.Width,bn?)32Height),ImageLockMode.Readonly,PixelFormat.Format32bppArgb);Bitmapbmp24=newBitmap(bmp32.Width,bn32.Height,PixelFormat.Format24bppRgb);BitmapDatadata24=bmp24.LockBits(newRectangle(0,0,bnp24.Width,bn?)24.Height),ImageLockMode.WriteOnly,PixelFormat.Format24bppRgb);unsafebyte*ptr32=(byte*)data32.ScanO.ToPointer();byte*ptr24=(byte*)data24ScanO.ToPointer();for(in
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025清包工裝修合同范本
- 2025年度產品采購合同范本
- 2025年農業生產設備租賃合同范本
- 約定離婚賠償協議
- 2025合作合同 電子產品收益分配協議書
- 2025燈光設備安裝合同樣本
- 不盡贍養義務協議書
- 西安郵電大學《工程招標投標與合同管理》2023-2024學年第二學期期末試卷
- 中國科學院大學《現代土木工程理論新進展》2023-2024學年第二學期期末試卷
- 四川文化傳媒職業學院《神奇的普洱茶》2023-2024學年第一學期期末試卷
- 第三講 酒店組織管理
- ge680ct用戶學習-技術手冊
- 小學生安全教育完整課件
- 陜旅版四年級英語下冊Unit-5-Where-Are-You-Going第2課時課件
- 畢業設計(論文)-巴哈賽車懸架系統設計
- 招銀國際投資銀行業務介紹課件
- 富余水深與船體下沉量的關系
- 壓力罐區球罐安裝工程無損檢測施工方案
- DB42T1915-2022三峽庫區園地面源污染防控技術指南-(高清最新)
- 幼兒園:中班科學:《會跳舞的小球》
- 結婚登記申請表
評論
0/150
提交評論