Copy Image TO Clipboard problem

Just starting out? Need help? Post your questions and find answers here.
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Copy Image TO Clipboard problem

Post by Dr_Pixel »

I did a search, but found only threads about getting an image FROM the Clipboard, and I'm having a very strange problem trying to copy an image to the clipboard - I hope someone can help...

In my program, I show a list of .bmp files in a folder - the user can click on the name of a file, and see it displayed in a child window - this works fine, all the bmp's are properly displayed.

Now, I have a toolbar button which can be clicked to send the displayed image to the clipboard, to paste into another program. But, sometimes it works, sometimes not.....

Here is my code:

Code: Select all

CopyImage(2,1)
UseImage(1)
SetClipboardData(#PB_ClipboardImage,ImageID())
OK, so I open PaintShopPro, then copy an image from my program.

Now, I paste it into PSP - sometimes it works, sometimes I get an error "can't read clipboard data", and sometimes nothing happens at all....
And sometimes when it doesn't work, I get a message from Windows that my program has caused an invalid page fault......

Oh, and the reason for copying the original image (image #2) into a new image (image #1) first, is because when I just copy the original image to the clipboard, when I try to paste the non-working ones into PSP, not only do they not paste, but also the image displayed in my program's window (in an image gadget) would disappear....

So, I next tried Irfanview - here, all the images will "paste" in, but the ones that don't paste into PSP come into Irfanview as just tall, thin, all black 2 colour images (my desktop is 32bit)

Any ideas?

This isn't random, either - it seems to be always the same images that work, and the same ones that don't work. Is there such a thing as a "bad" bmp, that will display ok in an image gadget, but won't copy to the clipboard?
Dr Pixel
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Dr_Pixel,

It would be purhaps easier seeing a larger part of lines to understand what goes wrong.

On my side, I tested SetClipboardData using any available type of images from UseEC_OLEImageDecoder() and the clipboard content is OK when calling Photoshop.

I do not make more tests, but maybe you have such bugs because the image bmp support is not OK or maybe because you are using this clipboard copy in a loop that does not let time enough to the clipboard to get data before doing something else.

In such a case, did you make any test placing a Delay or Sleep_() before looping in your app ?

Well just 2 ideas there ...

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Post by Dr_Pixel »

Thanks for the suggestions

I didn't mention in my post, but I had tried using delay(), both before and after the copying. But it didn't help.

Your post got me thinking to try using the ClearClipboard() command first, before copying the image, and that fixed it :D


This brings another question - I noticed that if I copy an image to the clipboard, and then end my program, that I can still paste this image into other programs later...

Does this mean that PB is NOT freeing the images when my program ends?

Well, really the question is should I free all images, etc, when my program ends?

I had thought this was done automatically.
Dr Pixel
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

Dr_Pixel,

PureBasic frees images, but the clipboard is a Windows object that stil contain data after closing any regular app ... just for copying data later !

This is the good way to do not clearing it.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Post by syntax error »

What to see if this works?

CLIPBOARD TESTER
Image

Code: Select all

; Clipboard Tester

#Image0=0
#Image1=1

Procedure RandomPattern(ImageNumber)
  UseImage(ImageNumber)
  StartDrawing(ImageOutput())
  w=ImageWidth() : h=ImageHeight()
  Box(0,0,w,h,0)
  For l=1 To 50
    FrontColor(Random(255),Random(255),Random(255))
    LineXY( Random(w),Random(h),Random(w),Random(h))
    Circle(Random(w),Random(h),Random(10))
  Next
  StopDrawing()
EndProcedure  

Procedure InitImage(ImageNumber)
  CreateImage(ImageNumber,240,180)
  UseImage(ImageNumber)
  StartDrawing(ImageOutput())
  Box(0,0,ImageWidth(),ImageHeight(),RGB(150,10,30))
  StopDrawing()
EndProcedure

CreateImage(#Image0,240,180)
InitImage(#Image1)


;- Clipboard functions

; send image to clipboard
Procedure ClipboardPutImage(ImageNumber)
  Protected clip
  If OpenClipboard_(0)
    EmptyClipboard_()
    clip=CopyImage(ImageNumber,#PB_Any)
    SetClipboardData_(#CF_BITMAP,UseImage(clip))
    FreeImage(clip)
    CloseClipboard_()
  EndIf
EndProcedure

; get image from clipboard - some code courtesy of 'freak'
Procedure.l ClipboardGetImage(ImageNumber)
  Protected hBitmap.l
  Protected *lptr.LONG, *wptr.WORD, BitmapData.BITMAP
  If OpenClipboard_(0)
    If IsClipboardFormatAvailable_(#CF_BITMAP)
      hBitmap=GetClipboardData_(#CF_BITMAP)
    EndIf
    CloseClipboard_()
  EndIf
  If hBitmap
    GetObject_(hBitmap, SizeOf(BITMAP), @BitmapData) 
    CreateImage(ImageNumber, 10, 10)
    DeleteObject_(ImageID()) 
    !extrn _PB_Image_CurrentObject 
    !mov eax, [_PB_Image_CurrentObject] 
    !mov [esp+8], eax 
    *lptr\l = hBitmap 
    *wptr = *lptr + 4 
    *wptr\w = BitmapData\bmWidth 
    *wptr + 2 
    *wptr\w = BitmapData\bmHeight 
    *wptr + 2 
    *wptr\w = BitmapData\bmBitsPixel 
    *lptr = *wptr + 2 
    *lptr\l = BitmapData\bmBits
  EndIf
  ProcedureReturn hBitmap
EndProcedure


IncludeFile "gui.pb"
Open_win()

RandomPattern(#Image0)
SetGadgetState(#pbimage,UseImage(#Image0))
SetGadgetState(#cbimage,UseImage(#Image1))

;- Main loop
Repeat

  ev=WaitWindowEvent()
;;;  Debug ev

  Select ev
    ; GADGET
    Case #PB_Event_Gadget
      Select EventGadgetID()
        Case #bt_randomize
          RandomPattern(#Image0)
          SetGadgetState(#pbimage,UseImage(#Image0))
        Case #bt_pasteimage
          ClipboardPutImage(#Image0)
        Case #bt_getimage
          imgHandle=ClipboardGetImage(#Image1)
          If imgHandle<>0
            If ImageWidth()>240 Or ImageHeight()>180
              ResizeImage(#Image1,240,180)
            EndIf
            SetGadgetState(#cbimage,UseImage(#Image1))
          Else
            MessageRequester("Error","No image in clipboard")
            InitImage(#Image1)
          EndIf
      EndSelect
    Case #PB_Event_Repaint
      SetGadgetState(#pbimage,UseImage(#Image0))
      SetGadgetState(#cbimage,UseImage(#Image1))
    Case 49318 ; window activated/de-activated
      SetGadgetState(#pbimage,UseImage(#Image0))
      SetGadgetState(#cbimage,UseImage(#Image1))
    ; CLOSEWINDOW
    Case #PB_EventCloseWindow
      quit=1
    Case 258 ; Key pressed
      quit=1
  EndSelect
  ;
Until quit=1

End
The required GUI.pb include:

Code: Select all

; Clipboard Tester - GUI

Enumeration
  #win
  #frame0
  #frame1
  #pbimage
  #cbimage
  #bt_randomize
  #bt_pasteimage
  #bt_getimage
EndEnumeration

Procedure Open_win()
  If OpenWindow(#win, 243, 86, 599, 327,  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar , "Clipboard Tester")
    If CreateGadgetList(WindowID())
      Frame3DGadget(#frame0, 30, 30, 260, 280, "PureBasic image")
       ImageGadget(#pbimage, 40, 50, 240, 180, Image0, #PB_Image_Border)
       ButtonGadget(#bt_randomize, 70, 250, 170, 20, "Randomize image")
       ButtonGadget(#bt_pasteimage, 70, 280, 170, 20, "Copy to clipboard")
      Frame3DGadget(#frame1, 310, 30, 260, 280, "Clipboard image")
       ImageGadget(#cbimage, 320, 50, 230, 180, Image1, #PB_Image_Border)
       ButtonGadget(#bt_getimage, 360, 250, 170, 40, "Grab image from clipboard")
    EndIf
  EndIf
EndProcedure
Dr_Pixel
User
User
Posts: 36
Joined: Fri Oct 24, 2003 1:36 pm

Post by Dr_Pixel »

I tried your code, but it doesn't work here.

I have Windows98, if that matters?

Anyway, nothing goes to the clipboard, and nothing comes back.

The randomize button does work, but nothing else.

Also, the buttons don't show on the window until I click on the place where they should be.
Dr Pixel
Post Reply