這兩天在弄 uEye CCD 終於成功了,但也告訴我必須要放棄
由於用的CCD是黑白的CCD,如果是彩色那還沒問題,但問題是黑白
在VB.net中提供的Bitmap的格式並沒有8 Byte的灰階 只有 8byte 指標的256色
由記體體中取出的資料是沒問題的
由uEye提供的Function取得指標
dim lpt as lptPtn
iss_GetImageMem(1, lpt) <----取得指標
再用
bmp2 = New Bitmap(640, 480, 640, Imaging.PixelFormat.Format8bppIndexed, lpt)
將記憶體的資料存到 bitmap
由於沒有8 bytes 的灰階 存的圖顏色會有問題
又不能直接修改 pixel 的顏色
必須先轉換到 24RGB
Dim bmp4 = New Bitmap(640, 480, Drawing.Imaging.PixelFormat.Format24bppRgb)
g1 = Graphics.FromImage(bmp4)
g1.PageUnit = GraphicsUnit.Pixel
g1.DrawImageUnscaled(bmp2, 0, 0)
再轉換為灰階
bmp1= GetGrayBitmap(bmp4)
Private Function GetGrayBitmap(ByVal bmp As Bitmap)
For Me.i = 0 To bmp.Width - 1
For Me.j = 0 To bmp.Height - 1
co = bmp.GetPixel(i, j)
gray = (co.R * 0.3 + co.G * 0.59 + co.B * 0.11)
bmp.SetPixel(i, j, Color.FromArgb(gray, gray, gray))
Next
Next
Return bmp
End Function
要的目的已經做到了,Londing非常重 系統會鈍鈍的,這還是縮小畫格
用原來的1280*1024,系統還會當 目前放棄動態量測的功能.........................
ps 一開始是用
Dim bytes As Integer = 640 * 480
Private Shared test(640 * 480) As Byte
Dim lpt As IntPtr
Dim rect As New Rectangle(0, 0, 640, 480)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp2.LockBits(rect, _Drawing.Imaging.ImageLockMode.ReadWrite, bmp2.PixelFormat)
iss_GetImageMem(1, lpt)
ptr = bmpData.Scan0
System.Runtime.InteropServices.Marshal.Copy(lpt, test, 0, bytes)
System.Runtime.InteropServices.Marshal.Copy(test, 0, ptr, bytes)
bmp2.UnlockBits(bmpData)
用兩次指標 由記憶體中先讀到陣列 test 再存到 bitmap 速度比較慢
由於用的CCD是黑白的CCD,如果是彩色那還沒問題,但問題是黑白
在VB.net中提供的Bitmap的格式並沒有8 Byte的灰階 只有 8byte 指標的256色
由記體體中取出的資料是沒問題的
由uEye提供的Function取得指標
dim lpt as lptPtn
iss_GetImageMem(1, lpt) <----取得指標
再用
bmp2 = New Bitmap(640, 480, 640, Imaging.PixelFormat.Format8bppIndexed, lpt)
將記憶體的資料存到 bitmap
由於沒有8 bytes 的灰階 存的圖顏色會有問題
又不能直接修改 pixel 的顏色
必須先轉換到 24RGB
Dim bmp4 = New Bitmap(640, 480, Drawing.Imaging.PixelFormat.Format24bppRgb)
g1 = Graphics.FromImage(bmp4)
g1.PageUnit = GraphicsUnit.Pixel
g1.DrawImageUnscaled(bmp2, 0, 0)
再轉換為灰階
bmp1= GetGrayBitmap(bmp4)
Private Function GetGrayBitmap(ByVal bmp As Bitmap)
For Me.i = 0 To bmp.Width - 1
For Me.j = 0 To bmp.Height - 1
co = bmp.GetPixel(i, j)
gray = (co.R * 0.3 + co.G * 0.59 + co.B * 0.11)
bmp.SetPixel(i, j, Color.FromArgb(gray, gray, gray))
Next
Next
Return bmp
End Function
要的目的已經做到了,Londing非常重 系統會鈍鈍的,這還是縮小畫格
用原來的1280*1024,系統還會當 目前放棄動態量測的功能.........................
ps 一開始是用
Dim bytes As Integer = 640 * 480
Private Shared test(640 * 480) As Byte
Dim lpt As IntPtr
Dim rect As New Rectangle(0, 0, 640, 480)
Dim bmpData As System.Drawing.Imaging.BitmapData = bmp2.LockBits(rect, _Drawing.Imaging.ImageLockMode.ReadWrite, bmp2.PixelFormat)
iss_GetImageMem(1, lpt)
ptr = bmpData.Scan0
System.Runtime.InteropServices.Marshal.Copy(lpt, test, 0, bytes)
System.Runtime.InteropServices.Marshal.Copy(test, 0, ptr, bytes)
bmp2.UnlockBits(bmpData)
用兩次指標 由記憶體中先讀到陣列 test 再存到 bitmap 速度比較慢
文章標籤
全站熱搜

您好,想跟您請教如何存圖(format16bppGrayScale),我有寫一個測試程式,但總是會有記憶體出錯的問題,有勞您的指點。 Private Sub FrmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim a(1600* 1500 -1) as Integer CreateBitmapFromRawDataBuffer(a) End Sub Public Function CreateBitmapFromRawDataBuffer(ByVal buffer As Integer()) As Bitmap Try '// Prepare required image's metadata. Dim imageSize As Size = New Size(1600, 1500) '; // Known size. Dim imagePixelFormat As System.Drawing.Imaging.PixelFormat = System.Drawing.Imaging.PixelFormat.Format16bppGrayScale '; // Known type. '// Set bitmap known image's metadata. Dim bm As Bitmap = New Bitmap(imageSize.Width, imageSize.Height, imagePixelFormat) '// Prepare working rectangle. Dim wholeBitmap As Rectangle = New Rectangle(0, 0, bm.Width, bm.Height) '// Lock all bitmap's pixels. Dim bd As System.Drawing.Imaging.BitmapData = bm.LockBits(wholeBitmap, System.Drawing.Imaging.ImageLockMode.ReadWrite, imagePixelFormat) '// Copy the buffer into bitmapData. System.Runtime.InteropServices.Marshal.Copy(buffer, 0, bd.Scan0, buffer.Length) '// Unlock all bitmap's pixels. bm.UnlockBits(bd) Return bm Catch ex As Exception Return Nothing End Try End Function