|
分類:[VB.NET/VB2005 以降]
お世話になっております.
VB2005で画像処理のアプリケーションを作成しています. 主に8bitグレー画像を使用します.
今まではvb6を使用しており,StretchDIBitsを用いてbyte配列の画像データをピクチャーボックスに表示していました. 同じような方法でvb2005で表示したいと思っています. 下記にコードを記載します. Form1_Load内のdepth=3の時は,bDataに作成した画像データが表示できます. しかし,depth=1の時は思っている画像データが表示されません. BmpInfo.bmiHeader と BmpInfo.bmiColorsが連続していないから?なのかよくわかりません. お手数ではございますがご教授頂けると幸いです。
Imports System.Runtime.InteropServices
Public Class Form1
Public Declare Function StretchDIBits Lib "gdi32" ( _ ByVal hDC As IntPtr, _ ByVal X As Integer, _ ByVal Y As Integer, _ ByVal dx As Integer, _ ByVal dy As Integer, _ ByVal SrcX As Integer, _ ByVal SrcY As Integer, _ ByVal wSrcWidth As Integer, _ ByVal wSrcHeight As Integer, _ ByVal npBits As Byte(), _ ByRef lpBitsInfo As BITMAPINFOHEADER, _ ByVal wUsage As Integer, _ ByVal tmp As Integer) As Integer
Public Const SRCCOPY = &HCC0020 Public Const DIB_RGB_COLORS = 0
Public Structure BITMAPINFO Dim bmiHeader As BITMAPINFOHEADER Dim bmiColors() As RGBQUAD End Structure
Public Structure BITMAPINFOHEADER Dim biSize As Integer Dim biWidth As Integer Dim biHeight As Integer Dim biPlanes As Short Dim biBitCount As Short Dim biCompression As Integer Dim biSizeImage As Integer Dim biXPelsPerMeter As Integer Dim biYPelsPerMeter As Integer Dim biClrUsed As Integer Dim biClrImportant As Integer End Structure
Public Structure RGBQUAD Dim rgbBlue As Byte Dim rgbGreen As Byte Dim rgbRed As Byte Dim rgbReserved As Byte End Structure
Public BmpInfo As BITMAPINFO 'ビットマップインフォ Dim nW As Integer = 512 Dim nH As Integer = 512 Dim bData() As Byte
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim gp As Graphics = PictureBox1.CreateGraphics Dim hDC As IntPtr = gp.GetHdc()
'オブジェクトへ画像を表示 Call StretchDIBits(hDC, _ 0, 0, BmpInfo.bmiHeader.biWidth, BmpInfo.bmiHeader.biHeight, _ 0, 0, BmpInfo.bmiHeader.biWidth, BmpInfo.bmiHeader.biHeight, _ bData, BmpInfo.bmiHeader, DIB_RGB_COLORS, SRCCOPY) Marshal.Release(hDC) gp.Dispose() End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim i, j As Integer Dim depth As Integer = 3 'Dim depth As Integer = 1 'グレー画像の時
With BmpInfo With .bmiHeader .biSize = Marshal.SizeOf(BmpInfo.bmiHeader) .biWidth = nW .biHeight = nH .biBitCount = 8 * depth .biPlanes = 1 .biCompression = 0 .biSizeImage = nW * nH * depth End With
ReDim .bmiColors(255)
For i = 0 To 255 .bmiColors(i).rgbBlue = i .bmiColors(i).rgbGreen = i .bmiColors(i).rgbRed = i .bmiColors(i).rgbReserved = 0 Next End With
ReDim bData(nH * nW * depth - 1) For i = 0 To nH * nW - 1 For j = 0 To depth - 1 bData(i * depth + j) = i Mod 256 Next Next
End Sub End Class
|