Coping Bitmap from Clipboard

Forums for specific tips, techniques and example code
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Coping Bitmap from Clipboard

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:43 pm

by synergyfocus >> Thu, 5 Jun 2003 0:11:02 GMT

Hi,
Does anyone know how to retrieve a bitmap from the clipboard and place in in a picture control?

I have got so far and now become stuck... Heres what I have got so far :

call openClipboard(null);
//opens the clipboard
write call isClipboardFormatAvailable(CF_BITMAP); //check for me just to ensure the type of data on the clipborad is a bitmap
handle := call getClipboardData(CF_BITMAP); //gets the handle to data stored on the clipboard
locked := call globalLock(handle);
//locks the data ready to retrieve it
What goes in here...!??!??!
call globalUnlock(locked);
//releases the lock
call closeClipboard;
//close the clipboard

I can't figure out how to now copy the data to my picture control.
Any ideas??

External functions are :
openClipboard(hwnd :Integer): Integer is OpenClipboard in user32; isClipboardFormatAvailable(format: Integer): Integer is IsClipboardFormatAvailable in user32;
getClipboardData(format: Integer): Integer is GetClipboardData in user32; globalLock(handle: Integer): Integer is GlobalLock in kernel32; globalUnlock(handle :Integer): Integer is GlobalUnlock in kernel32; closeClipboard(): Integer is CloseClipboard in user32;

Thanks
Stephen Persson

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Coping Bitmap from Clipboard

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:43 pm

by allistar >> Thu, 5 Jun 2003 9:21:57 GMT

Hi Steve,
I would imagine that "getClipboardData" will return an Integer that is the memory address of a buffer that contains the data. I don't know how to access that data directly in Jade but you should be able to write a C/C++ external function that does it, something like this:

extern "C" __declspec(dllexport) long JOMAPI getBinaryFromBuffer(char *image) {

HANDLE hData;
LPVOID pData;
if (!IsClipboardFormatAvailable(CF_TEXT))
return;
OpenClipboard();
hData = GetClipboardData(CF_TEXT);
pData = GlobalLock(hData);
strcpy(image, (LPSTR)pData);
GlobalUnlock(hData);
CloseClipboard();
}

You would have to make sure you pass through a binary that is long enough into this function. The external function in Jade would look something like this:

getBinaryFromClipboard(image: Binary[10485760] output) is getBinaryFromBuffer in someDll;

That allows for images of up to 10Mb. If you can't guarantee that they will be smaller than that then you will have to get at the data in chunks. You would then then to make a dll function that returned the length of the clipboard data like this:

return strlen(pData) + 1;

You should then be able to set the image of the picture from the binary you get out of the clipboard.

I hope this helps,
Allistar.

------------------------------------------------------------------
Allistar Melville
Software Developer, Analyst allistar@silvermoon.co.nz
Auckland, NEW ZEALAND

Silvermoon Software
Specialising in JADE development and consulting
Visit us at: http://www.silvermoon.co.nz
*NEW* Simple web access to Jade at: www.silvermoon.co.nz/jhp.html ------------------------------------------------------------------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Coping Bitmap from Clipboard

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:43 pm

by michaelj >> Thu, 5 Jun 2003 14:25:07 GMT

You might be able to use something like;

getBinaryFromPos( imageBin : Binary[10485760] output; positionFrom:Integer):Integer is lstrcpy in kernel32;

The problem when using this is to know where the binary ends, as I dont believe lstrlen (also in kernel32) will return the logical end of a binary.

I havent used it for binaries before but I dont see why it shouldnt work if you can get round this problem.

Michael J

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Coping Bitmap from Clipboard

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:43 pm

by synergyfocus >> Thu, 5 Jun 2003 21:41:06 GMT

You are right in saying that the function lstrlen only works for strings, otherwise it just returns 0. I have also been trying to use the RtlMoveMemory (also in kernel32) to move the memory to my binary variable:

moveMemory(pDst, pSrc, byteLen : Integer) is RtlMoveMemory in kernel32;

then my method
constants
WM_CUT = #300;
WM_COPY = #301;
WM_PASTE = #302;
WM_CLEAR = #303;
WM_UNDO = #304;
CF_BITMAP = #2;
vars
locked, handle : Integer;
str : String;
binary : Binary[10000];begin

call openClipboard(null);
handle := call getClipboardData(CF_BITMAP);
locked := call globalLock(handle);
call moveMemory(binary.bufferAddress, handle, 10000);
call globalUnlock(locked);
call closeClipboard;
write binary;
epilog

end;

But this gives an exception everytime - presumably because the number of bytes I asked it to move does not exactly match the number of bytes actually on the clipboard.
So again the issue comes back to - how do I find out the length of the binary on the clipboard?

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Coping Bitmap from Clipboard

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:43 pm

by jcampbell >> Thu, 5 Jun 2003 22:16:18 GMT

If you have a handle to the bitmap via GetClipBoardData(CF_BITMAP) you can call GetObject() to get the bitmap's attributes including size.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Coping Bitmap from Clipboard

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:43 pm

by allistar >> Fri, 6 Jun 2003 1:39:49 GMT

Using:

OpenClipboard(NULL);
hBitmap = (HBITMAP)GetClipboardData(CF_BITMAP);
CloseClipboard();

gets you a handle to the bitmap, as a HBITMAP structure.

Take a look at this link which shows how to read into the bitmap structure to tell how long it is:

http://home.earthlink.net/~danrollins/e ... tation.htm

Yo may find an easier way from a coding point of view is to save the bitmap that comes from the clipboard into a temporary and then get Jade to load that file into the picture control. That way you don't need to worry about what colour depth the image in the clipboard is.

Regards,
Allistar.

------------------------------------------------------------------
Allistar Melville
Software Developer, Analyst allistar@silvermoon.co.nz
Auckland, NEW ZEALAND

Silvermoon Software
Specialising in JADE development and consulting
Visit us at: http://www.silvermoon.co.nz
*NEW* Simple web access to Jade at: www.silvermoon.co.nz/jhp.html ------------------------------------------------------------------

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Coping Bitmap from Clipboard

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:43 pm

by Anonymous >> Mon, 30 Jun 2003 13:05:13 GMT

Stephen,
Ive noticed that the jade full install puts the imageman DLL into the bin directory. I have used it in the past for tiff processing and wih a quick look I reckon the following should generate a file which can then be loaded into your picture control as normal. The only catch would be licensing, but It is included in the Jade install so is it already licensed?

declare;

imgXWriteImage( fileName : String ; fileExt : String ;fileHandle,imageblock,mainWin,flags : Integer) is ImgXWriteImage in imgman32;

and

imgFromClipboard():Integer is ImgFromClipboard in imgman32;

then a call like this will generate an image file;

call imgXWriteImage( 'C:\asd.bmp', 'bmp' , call imgFromClipboard , null , null , 0);


Michael J


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 13 guests