我此刻正在挣扎,并希望有人可以帮助我。
我从SQL数据库中获取图像并将其存储到BYTE []。 现在我必须将其转换为CImage以在我的程序中显示它。
我很乐意为你提供帮助。
继承我的守则:
SQLLEN cbData;
CImage image;
BYTE testByte[arrSize];
try
{
m_Rc = ExecuteDirect(SQLStmt_Select, "SELECT ImgDat FROM ImgMap", "ImgTyp = 'AtlStm.AtlImg'", Empty_String, Empty_String, Empty_String, Empty_String);
m_RcFetch = SQLFetch(m_Hstmt);
m_Rc = SQLGetData(m_Hstmt, 1, SQL_C_BINARY, &testByte, arrSize, &cbData);
}
// store testByte in image
在C#中,我只是将Byte []写入MemoryStream并使用它来创建一个Image。但在C ++中我完全不知道。
我的C#代码:
DataTable dt = new DataTable();
OdbcConnection cn = new OdbcConnection(strCn);
cn.Open();
OdbcDataAdapter adapter = new OdbcDataAdapter(sqlCommand, cn);
DataSet ds = new DataSet();
adapter.Fill(ds, "ImgMap");
int c = ds.Tables["ImgMap"].Rows.Count;
if (c > 0)
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["ImgMap"].Rows[c - 1]["ImgDat"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
pictureBox1.Image = Image.FromStream(stmBLOBData);
}
cn.Close();
修改 这是我当前的代码,它可以工作,但我的控制权仍然是空的。
BOOL DBStatement::GetSqlData_Image(CWnd* parent, CImage image, CStatic* dc)
{
SQLLEN cbData;
BYTE testByte[2048];
int width = 312;
int height = 159;
void* bits;
try
{
m_Rc = ExecuteDirect(SQLStmt_Select, "SELECT ImgDat FROM ImgMap", "ImgTyp = 'AtlStm.AtlImg'", Empty_String, Empty_String, Empty_String, Empty_String);
m_RcFetch = SQLFetch(m_Hstmt);
m_Rc = SQLGetData(m_Hstmt, 1, SQL_C_BINARY, &testByte, 2048, &cbData);
BITMAPFILEHEADER* bmfh;
bmfh = (BITMAPFILEHEADER*)testByte;
BITMAPINFOHEADER* bmih;
bmih = (BITMAPINFOHEADER*)(testByte + sizeof(BITMAPFILEHEADER));
BITMAPINFO* bmi;
bmi = (BITMAPINFO*)bmih;
bits = (void*)(testByte + bmfh->bfOffBits);
CDC *screenDC = AfxGetMainWnd()->GetDC();
CDC mDC;
mDC.CreateCompatibleDC(screenDC);
CBitmap b;
b.CreateCompatibleBitmap(screenDC, 300, 150);
CBitmap *pob = mDC.SelectObject(&b);
mDC.SetStretchBltMode(HALFTONE);
CRect rectDest(0, 0, width, height);
image.StretchBlt(mDC.m_hDC, rectDest);
mDC.SelectObject(pob);
HBITMAP hbmp = CreateDIBitmap(mDC.m_hDC, bmih, CBM_INIT, bits, bmi, DIB_RGB_COLORS);
b.Attach(hbmp);
dc->SetBitmap((HBITMAP)b.Detach());
}