iammic 無聊寫了一個將圖檔轉到 Excel 的程式,去抓取圖檔的像素顏色將顏色設定到 Excel 儲存格

中,但 Excel 儲存格可縮放的大小有限,程式整體效率也還不太好,倒是從中又學了一些 .Net 中

Excel 的程式用法。(要開始撰寫程式時可以參考 VB.NET 簡單的 Excel 檔讀寫 ,設定 Excel 參考。)

 

程式結果畫面:

image

程式碼:

Dim image As New Bitmap("C:\test.jpg")
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange As Excel.Range
 
xlApp = New Excel.Application
xlApp.DisplayAlerts = False       
xlApp.Visible = True
xlBook = xlApp.Workbooks.Add()
 
'縮小 Excel Window
xlBook.Windows(1).Zoom = 10
 
xlSheet = xlBook.Sheets(1)
 
For i As Integer = 1 To image.Height - 1
    For j As Integer = 1 To image.Width - 1
 
        xlRange = xlSheet.Cells(i, j)
        xlRange.Interior.Color = ColorTranslator.ToWin32(image.GetPixel(j, i))
 
        '儲存格長寬設定
        xlRange.ColumnWidth = 2
        xlRange.RowHeight = 15
 
    Next j
Next i
xlBook.SaveAs("c:\1.xls")
xlBook.Close()
xlApp.Quit()
arrow
arrow
    全站熱搜

    iammic 發表在 痞客邦 留言(0) 人氣()