How to load and save an image from a SQL Server database using GDI + and C ++?

I need to specifically download a jpg image that was saved as a blob. GDI + makes it easy to extract images from files, but not from databases ...

+4
source share
2 answers

Take a look at Image :: Image (IStream *, BOOL) . This takes a pointer to a COM object that implements the IStream interface. You can get one of them by allocating some GlobalAlloc global memory and then calling CreateStreamOnHGlobal in the returned descriptor. It will look something like this:

shared_ptr<Image> CreateImage(BYTE *blob, size_t blobSize) { HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE,blobSize); BYTE *pImage = (BYTE*)::GlobalLock(hMem); for (size_t iBlob = 0; iBlob < blobSize; ++iBlob) pImage[iBlob] = blob[iBlob]; ::GlobalUnlock(hMem); CComPtr<IStream> spStream; HRESULT hr = ::CreateStreamOnHGlobal(hMem,TRUE,&spStream); shared_ptr<Image> image = new Image(spStream); return image; } 

But with error checking and such (omitted here to make things clearer)

+2
source

First select your blog in an array of bytes, then use something like this:

 public static Image CreateImage(byte[] pict) { System.Drawing.Image img = null; using (System.IO.MemoryStream stream = new System.IO.MemoryStream(pict)) { img = System.Drawing.Image.FromStream(stream); } return img; } 
0
source

Source: https://habr.com/ru/post/1277653/


All Articles