Random drag and drop don't resize, even when set to do so?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Random drag and drop don't resize, even when set to do so?

Post by Fangbeast »

Randomly, some images simply won't resize correctly when dropped on the image gadget even though that is what has been set to do so at "Case #PB_Event_GadgetDrop"

Most dragged and dropped images do obey that line but some don't and I don't know why.

Can anyone see the flaw in my code?

Code: Select all

; //  ------------------------------------------
; //  Beginning of visual designer created code //
; //  ------------------------------------------

;  //  Load the various needed plugins for this program  //

UsePNGImageDecoder()
UsePNGImageEncoder()
UseJPEGImageDecoder()

;  //  Visual designer created constants code and windows  // 

Define EventID, MenuID, GadgetID, WindowID

Enumeration 1
  #Window_iDrag
EndEnumeration

#WindowIndex = #PB_Compiler_EnumerationValue

Enumeration 1
  ; Window_iDrag
  #Gadget_iDrag_Myimage
  #Gadget_iDrag_Saveimage
  #Gadget_iDrag_Exitprogram
  #Gadget_iDrag_Messages
EndEnumeration

#GadgetIndex = #PB_Compiler_EnumerationValue

Enumeration 1
  #Image_iDrag_Myimage
EndEnumeration

#ImageIndex = #PB_Compiler_EnumerationValue

CatchImage(#Image_iDrag_Myimage,?_OPT_iDrag_Myimage)

;- Custom Resize Images

ResizeImage(#Image_iDrag_Myimage, 640, 480)

DataSection
  _OPT_iDrag_Myimage:
  IncludeBinary "Images\antelopes.jpg"
EndDataSection

Procedure.i Window_iDrag()
  If OpenWindow(#Window_iDrag,0,0,650,545,"Drag and drop an image on target from anywhere, then save to storage",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
      ImageGadget(#Gadget_iDrag_Myimage,5,5,640,480,ImageID(#Image_iDrag_Myimage),#PB_Image_Border|#PB_Image_Raised)
      ButtonGadget(#Gadget_iDrag_Saveimage,5,490,200,25,"Save image to storage")
        SetGadgetFont(#Gadget_iDrag_Saveimage,LoadFont(#Gadget_iDrag_Saveimage,"Comic Sans MS",10))
      ButtonGadget(#Gadget_iDrag_Exitprogram,445,490,200,25,"Exit program immediately")
        SetGadgetFont(#Gadget_iDrag_Exitprogram,LoadFont(#Gadget_iDrag_Exitprogram,"Comic Sans MS",10))
      TextGadget(#Gadget_iDrag_Messages,5,520,640,20,"Messages")
        SetGadgetColor(#Gadget_iDrag_Messages,#PB_Gadget_FrontColor,$800000)
        SetGadgetFont(#Gadget_iDrag_Messages,LoadFont(#Gadget_iDrag_Messages,"Comic Sans MS",9,#PB_Font_Bold))
      HideWindow(#Window_iDrag,#False)
    ProcedureReturn WindowID(#Window_iDrag)
  EndIf
EndProcedure

; //  ------------------------------------------
; //  End of visual designer created code //
; //  ------------------------------------------


;  //  All program specific variables, structures etc  //

Global CurrentDirectory.s = GetCurrentDirectory()
Global ImageDirectory.s   = CurrentDirectory.s  + "Storage\Images\"
Global DroppedImageId.i

Declare   SaveDroppedImage()

; //  Save the image that was dropped on the imagegadget //

Procedure SaveDroppedImage()
  If IsImage(DroppedImageId.i)
    ImageToSave.i = CreateImage(#PB_Any, GadgetWidth(#Gadget_iDrag_Myimage), GadgetHeight(#Gadget_iDrag_Myimage))
    If IsImage(ImageToSave.i)
      If StartDrawing(ImageOutput(ImageToSave.i))
        DrawImage(ImageID(DroppedImageId.i), 0, 0)
        StopDrawing()
      EndIf
      RandomFileName.s  = Str(Random(2000, 1))
      SaveImage(ImageToSave.i, ImageDirectory.s + RandomFileName.s  + ".png", #PB_ImagePlugin_PNG)
      SetGadgetText(#Gadget_iDrag_Messages,  "Saved new image: " + ImageDirectory.s + RandomFileName.s  + ".png")
    EndIf
    DroppedImageId.i    = #False
    If IsImage(DroppedImageId.i)
      FreeImage(DroppedImageId.i)
    EndIf
    If IsImage(ImageToSave.i)
      FreeImage(ImageToSave.i)
    EndIf
  Else
    SetGadgetText(#Gadget_iDrag_Messages,  "No image has been dropped as yet, default is still in the frame")
  EndIf
EndProcedure

; //  //

If Window_iDrag()

  Define quitiDrag = #False
  
  ResizeGadget(#Gadget_iDrag_Myimage, #PB_Ignore,  #PB_Ignore,  DesktopScaledX(640),   DesktopScaledY(480))
  ResizeImage(#Image_iDrag_Myimage,                             DesktopScaledX(640),   DesktopScaledY(480))
  SetGadgetState(#Gadget_iDrag_Myimage,                         ImageID(#Image_iDrag_Myimage))

  EnableGadgetDrop(#Gadget_iDrag_Myimage, #PB_Drop_Image, #PB_Drag_Copy)

  Repeat
    
    EventID  = WaitWindowEvent()
    MenuID   = EventMenu()
    GadgetID = EventGadget()
    WindowID = EventWindow()
    
    Select EventID
        
      Case #PB_Event_CloseWindow
        Select WindowID
          Case #Window_iDrag
            quitiDrag = #True
        EndSelect
        
      Case #PB_Event_GadgetDrop
        Select GadgetID
          Case #Gadget_iDrag_Myimage
            ; Get the image id of the image that was dropped from anywhere
            DroppedImageId.i = EventDropImage(#PB_Any)
            ResizeGadget(#Gadget_iDrag_Myimage, #PB_Ignore,  #PB_Ignore,  DesktopScaledX(640),   DesktopScaledY(480))
            ResizeImage(DroppedImageId.i,                                       DesktopScaledX(640),   DesktopScaledY(480), #PB_Image_Smooth)
            SetGadgetState(#Gadget_iDrag_Myimage,                               ImageID(DroppedImageId.i))
            SetGadgetText(#Gadget_iDrag_Messages, "Image was dropped from somewhere with an ID of: "  + Str(DroppedImageId.i))
        EndSelect
        
      Case #PB_Event_Gadget
        Select GadgetID
          Case #Gadget_iDrag_Saveimage
            SaveDroppedImage()
          Case #Gadget_iDrag_Exitprogram
            quitiDrag = #True
        EndSelect
        
    EndSelect
    
  Until quitiDrag
  
  CloseWindow(#Window_iDrag)
  
EndIf

End
Amateur Radio, D-STAR/VK3HAF
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: Random drag and drop don't resize, even when set to do s

Post by BarryG »

I can't drag ANY images onto it at all - I just get the "Denied" mouse cursor. So I can't even begin to help. Win 10. Which OS are you using?
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Random drag and drop don't resize, even when set to do s

Post by Fangbeast »

BarryG wrote:I can't drag ANY images onto it at all - I just get the "Denied" mouse cursor. So I can't even begin to help. Win 10. Which OS are you using?
Interesting. I used to have that happen to me after some windows updates and somewhere down the track, another windows update fixed it.
windows 10, x64.

Just went to https://www.pexels.com/search/background%20nice/ and dragged the first image that I saw onto the gadget, a picture of a brick wall and it worked fine except for the resizing which was ignored horizontally.

Dragged another image from another page on that site and it dropped just fine. Of course, my resizing is not being applied properly to the image so my logic is faulty somewhere
Amateur Radio, D-STAR/VK3HAF
infratec
Always Here
Always Here
Posts: 6818
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Random drag and drop don't resize, even when set to do s

Post by infratec »

Hm ...

the returnvalue of ResizeImage is 0 in such a case, which means it failed.
I simply dragged your user icon :mrgreen:

I check now a bit deeper what excatly why it fails.
infratec
Always Here
Always Here
Posts: 6818
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Random drag and drop don't resize, even when set to do s

Post by infratec »

Bugfix:

Code: Select all

Select GadgetID
          Case #Gadget_iDrag_Myimage
            ; Get the image id of the image that was dropped from anywhere
            DroppedImageId.i = EventDropImage(#PB_Any)
            NewImg = CopyImage(DroppedImageId, #PB_Any)
            FreeImage(DroppedImageId)
            
            ResizeImage(NewImg, DesktopScaledX(640), DesktopScaledY(480), #PB_Image_Smooth)
            SetGadgetState(#Gadget_iDrag_Myimage,                               ImageID(NewImg))
            SetGadgetText(#Gadget_iDrag_Messages, "Image was dropped from somewhere with an ID of: "  + Str(DroppedImageId.i))
        EndSelect
Something is wrong with the dropped image.
I think that's a PB bug.
User avatar
Bisonte
Addict
Addict
Posts: 1226
Joined: Tue Oct 09, 2007 2:15 am

Re: Random drag and drop don't resize, even when set to do s

Post by Bisonte »

I also think about a bug in PB. I do it nearly at the same way, but I draw the image onto a new image... I forgot the function CopyImage() ;)
PureBasic 6.04 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom​​
English is not my native language... (I often use DeepL to translate my texts.)
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Random drag and drop don't resize, even when set to do s

Post by Fangbeast »

Bisonte wrote:I also think about a bug in PB. I do it nearly at the same way, but I draw the image onto a new image... I forgot the function CopyImage() ;)
Bisonte, don't tell Infratec you fixed mine already (Shhhhhhh).

/me runs away and hides.

Wondered why the resizing was being ignored, got a few more grey hairs trying to figure out why existing code that works everywhere else didn't work here.

It's as if the API structure for the image control isn't being filled correctly? Don't know.
Amateur Radio, D-STAR/VK3HAF
infratec
Always Here
Always Here
Posts: 6818
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Random drag and drop don't resize, even when set to do s

Post by infratec »

There is only one point which matters:

you got a solution :!:

But the fix should be shown public, for others which runs into the same problem.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Random drag and drop don't resize, even when set to do s

Post by Fangbeast »

infratec wrote:There is only one point which matters:

you got a solution :!:

But the fix should be shown public, for others which runs into the same problem.
Well, if I post the working code, someone is going to bitch at me about not providing the graphics in the code so I will bundle it up next week and put it into my dropbox along witht he ooklist program George has been helping me with.
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Random drag and drop don't resize, even when set to do s

Post by Fangbeast »

Okay, here are the two things I have been playing with that will get added into my keeper program My family information management program) that George Bisonte has been heavily helping me with because there is so much that I don't understand still.

iDrag was a small sub program that allowed me to drag and drop graphics from *most* web pages to an image gadget. This gadget then allows you to automatically save these pictures to a directory automatically if you ticked the check box (to their original sizes) to a random filename.

Please note that some web sites obfuscate their code using javascript (or something else) to display images so you will not be able to drag anything.
Book list is just a small book library manager for my wife, nothing special. it was originally desiged to either manually add or edit books but then I added batch mode retrieval based on ISBN numbers. Designed at first to get books only from ISFDB, George updated it to use threads and also try google books and amazon books if the first one didn't return any data.

You can manually add covers if none are returned by loading them from disk or dragging and dropping them from the web (iDrag)
Now I have to add all of this to keeper (oh, my aching ass and back)

Thanks for the help George.

P.s. Windows 10 Pro x64, using the latest PB X86
Amateur Radio, D-STAR/VK3HAF
infratec
Always Here
Always Here
Posts: 6818
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Random drag and drop don't resize, even when set to do s

Post by infratec »

Since I did also not have the picture I simply use CreateImage() with #Red as backcolor.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Random drag and drop don't resize, even when set to do s

Post by Fangbeast »

infratec wrote:Since I did also not have the picture I simply use CreateImage() with #Red as backcolor.
Ah, but you are intelligent and can cope. Do you have any idea of the number of times people bitched at me when I give them something for free and they want all the graphics as well??? I have to always bundle everything up for the complainers.

My tiny brain can barely cope to code something that works:):):)

Anyway, this should also show your fix/George's fix:)::)
Amateur Radio, D-STAR/VK3HAF
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4747
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Random drag and drop don't resize, even when set to do so?

Post by Fangbeast »

Hahahaha, the program you helped me fix now no longer works again after the latest round of buggy as hell windows updates InfraTec. No code was changed since we fixed the program. All it took was a windows update.

I couldn't create directories, share files and a whole lot of other problems.

Reformatted, reinstalled, can create directories again (but still had to work for hours to get sharing working again) but image dragging and dropping is dead on this computer.

*Update* Sorry Bernd, I made some assumptions and got them wrong. All drag and drop functions now work and I have extensively reworked the program. *Whew* Now for some decaf
Amateur Radio, D-STAR/VK3HAF
Post Reply