アプリ版:「スタンプのみでお礼する」機能のリリースについて

いつも丁寧なご回答を頂きありがとうございます。
GetDIBits関数の使い方について質問させて頂きます。
http://msdn.microsoft.com/ja-jp/library/cc428673 …
上記サイトを参考にして色々と試してみてWin32APIでは正常に動作したのですが、C#で正常に動作しません。

<Win32API>
LPCOLORREF pScanData = NULL;
BITMAPINFOHEADER bmpinfo = {0};

pScanData = new COLORREF[ bmp.bmWidth ];
bmpinfo.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.biWidth = bmp.bmWidth;
bmpinfo.biHeight = bmp.bmHeight;
bmpinfo.biPlanes = 1;
bmpinfo.biBitCount = 32;
bmpinfo.biCompression = BI_RGB;

// 一行分画像データを DIB で取り出す
GetDIBits( hdc, hBitmap,1, 1, pScanData, (LPBITMAPINFO)&bmpinfo, DIB_RGB_COLORS );

特に第5、6引数に何を渡してやればいいのかがわかりません。
以上ご助言よろしくお願いいたします。

A 回答 (1件)

 こんばんは。


 IntPtrではないでしょうか。unsafeキーワードの使用も避ける事が出来なさそうです。
 後、通常は画像イメージが上下逆転しているので、上から1行分読み込む場合は「ビットマップの高さ-1」を指定します。以下参考程度に。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication
{
public partial class Form1 : Form
{
public const int BI_RGB = 0;
public const int DIB_RGB_COLORS = 0;

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct BITMAPINFOHEADER
{
public UInt32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public UInt16 biPlanes;
public UInt16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
};

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct BITMAPINFO
{
public BITMAPINFOHEADER bmih;
public fixed UInt32 bmiColors[1];
};

[DllImport("User32.Dll")]
public static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("User32.Dll")]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

[DllImport("gdi32.dll")]
public static extern int GetDIBits(IntPtr hdc, IntPtr hbmp, UInt32 uStartScan, UInt32 cScanLines, IntPtr lpvBits, IntPtr lpbi, UInt32 uUsage);

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
//ビットマップを読む
Bitmap bitmap = new Bitmap("test.bmp");
IntPtr hbitmap = bitmap.GetHbitmap();

//1行分の受け取りバッファを割り当てる
UInt32[] ui32Buffer = new UInt32[bitmap.Width];

unsafe
{
BITMAPINFO bi;
bi.bmih.biSize = (UInt32)sizeof(BITMAPINFOHEADER);
bi.bmih.biPlanes = (UInt16)1;
bi.bmih.biCompression = BI_RGB;
bi.bmih.biBitCount = 32;
bi.bmih.biWidth = bitmap.Width;
bi.bmih.biHeight = bitmap.Height;

IntPtr hdc = GetDC(IntPtr.Zero);
fixed (void* lpvBits = ui32Buffer)
{
//構造体へ詳細を取り込む
if (GetDIBits(hdc, hbitmap, 0, 0, IntPtr.Zero, new IntPtr(&bi), DIB_RGB_COLORS) == 0)
MessageBox.Show("error");

//1行分読み込む
if (GetDIBits(hdc, hbitmap, (UInt32)(bitmap.Height - 1), 1, new IntPtr(lpvBits), new IntPtr(&bi), DIB_RGB_COLORS) == 0)
MessageBox.Show("error");
}
ReleaseDC(IntPtr.Zero, hdc);
}

//データの確認
foreach (UInt32 ui32 in ui32Buffer)
{
Color color = Color.FromArgb((Int32)ui32);
Int32 R = color.R;
Int32 G = color.G;
Int32 B = color.B;
}
}
}
}
    • good
    • 0
この回答へのお礼

ご教授頂きありがとうございます。
すごく参考になりました。C#でGetDIBits関数を使う方法は解決しました。
ありがとうございました。^^感謝感謝です。
.netで使用される型がC++のどの型に対応しているのか分からなくて困っていたのですが下記のサイトに載っていました。
http://www.atmarkit.co.jp/fdotnet/dotnettips/024 …

お礼日時:2009/07/21 12:30

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!