I am trying to get the attached to work as a dll. I am fine with creating and using a dll it is the GDI I am not fine with. When it gets to the save of the image I always get an object busy message. Does anyone have any idea what the problem is?
Thanks
TonyM
Hi Tony,
This is the working version:
Sub SaveJPG(Win as WINDOW, Top as Int, Left as Int, Width as Int, Height as Int, SaveName as String),Int
GdiplusStartupInput gdistart
pointer MyBitmap ' type Bitmap is declared as a structure
EncoderParameters EncoderParms
ULONG_PTR gdiToken
HDC hdcWindow = GetHDC(Win)
HDC hdcImage = CreateCompatibleDC(hdcWindow)
HBITMAP hbm = SelectObject(hdcImage, CreateCompatibleBitmap(hdcWindow, Width, Height))
' copy window pixels to this bitmap
BitBlt(hdcImage, 0, 0, Width, Height, hdcWindow, Left, Top, SRCCOPY)
ReleaseHDC(Win, hdcWindow, FALSE)
hbm = SelectObject(hdcImage, hbm)
DeleteDC(hdcImage)
gdiStart.gdiplusVersion = 1
gdistart.DebugEventCallback = NULL
gdistart.SuppressBackgroundThread = FALSE
gdistart.SuppressExternalCodecs = FALSE
gdiplusStartup(&gdiToken,gdistart,Null)
_Error = GdipCreateBitmapFromHBITMAP(hbm, NULL, &MyBitmap)
DeleteObject(hbm)
If _Error
gdipErrors(_Error) : gdiplusshutdown(gdiToken)
Return _Error
Endif
int quality = QualityModeLow
EncoderParms.Count = 1
EncoderParms.Parameter[0].NumberOfValues = 1
EncoderParms.Parameter[0].Guid = EncoderQuality
EncoderParms.Parameter[0]._Type = EncoderParameterValueTypeLong
EncoderParms.Parameter[0].Value = &quality ' pointer to values
_Error = gdipSaveImageToFile(MyBitmap, s2w(SaveName), &ImageEncoderJpeg, &EncoderParms)
If _Error > 0
gdipErrors(_Error) : gdiplusshutdown(gdiToken)
Return _Error
Endif
gdiplusshutdown(gdiToken)
Return 0
EndSub
You Sir, are a genius. I have spent 2 days trying to work out what was wrong. Thank you Very Very Much.
Thanks
TonyM
Hi Sapero, Sorry to have to call upon you again. It sometimes seems as though I may as well ask you to write the whole thing.
I am still having a problem. The jpeg saves now but it is not picking up text in my window. The examples below are my original window with text and the result of the save. Is it because the text is in static controls?
Thanks
TonyM
Yes, this is what GetHDC is storing - only what you draw on the window.
If you replace GetHDC with GetDC (passing win.hwnd instead win), and replace ReleaseHDC with ReleaseDC (again win.hwnd), the screenshot will include all controls, but will clip invisible parts of the window, or include other windows covering your window.
Tried that and it loses the pictures. I have decided to create a dummy window and print the text on the window before saving it. It seems to work the way I want.
Thanks for all your help
TonyM
Is your image in a 2d/3d window? If so, GetDC+BitBlt will not capture it, but GetHDC will copy it from DirectX to the internal HDC (I think so), so BitBlt will include the image.
They're not completely lost but they are not in the position they should be. They seem to be shifted down.