image dimensions
Posted: Mon Feb 23, 2026 7:01 pm
I haven't found a way. How to read the width and height of the image?
The Cross-Platform Multimedia Application Layer
https://www.hollywood-mal.com/forums/
Code: Select all
LoadBrush(1,"c:\\23.png")
DebugPrint(GetAttribute(#BRUSH,1,#ATTRWIDTH))
DebugPrint(GetAttribute(#BRUSH,1,#ATTRHEIGHT))
after this which is obvious a LoadBrush() you can get image size1. Load the image and read the dimensions
Code: Select all
@APPTITLE "Load JPG and Drag Rectangle"
@DISPLAY {Width=800, Height=600, Title="Load JPG and Drag Rectangle", Color=$FFFFFF}
; Global variables
Global original_image
Global rect_x = (800 - 400) / 2
Global rect_y = (600 - 300) / 2
Global is_dragging = False
Global drag_offset_x, drag_offset_y
; Function to draw the rectangle and handles
Function DrawRectangle()
If original_image
; Draw the red rectangle
Box(rect_x, rect_y, 400, 300, $FF0000, #FILLNONE)
; Draw handles at the corners of the rectangle
Box(rect_x - 5, rect_y - 5, 10, 10, $0000FF) ; Top-left handle
Box(rect_x + 400 - 5, rect_y - 5, 10, 10, $0000FF) ; Top-right handle
Box(rect_x - 5, rect_y + 300 - 5, 10, 10, $0000FF) ; Bottom-left handle
Box(rect_x + 400 - 5, rect_y + 300 - 5, 10, 10, $0000FF) ; Bottom-right handle
EndIf
EndFunction
; Function to start dragging the rectangle
Function StartDragging(x, y)
If x >= rect_x And x <= rect_x + 400 And y >= rect_y And y <= rect_y + 300
is_dragging = True
drag_offset_x = x - rect_x
drag_offset_y = y - rect_y
EndIf
EndFunction
; Function to drag the rectangle
Function DragRectangle(x, y)
If is_dragging
; Clear the old rectangle by redrawing the image
If original_image
DisplayBrush(original_image, 0, 0)
EndIf
; Update the rectangle position
rect_x = x - drag_offset_x
rect_y = y - drag_offset_y
; Draw the new rectangle
DrawRectangle()
EndIf
EndFunction
; Function to stop dragging the rectangle
Function StopDragging()
is_dragging = False
EndFunction
; Function to load an image via dialog
Function LoadImage()
Local file$
file$ = FileRequest("Select a JPG Image", "JPG Files|jpg", 0)
If file$
DebugPrint("Selected path: ", file$)
original_image = LoadBrush(Nil, file$)
If original_image
DisplayBrush(original_image, 0, 0)
DrawRectangle()
DebugPrint("JPG image loaded successfully.")
Else
DebugPrint("Failed to load JPG image.")
SystemRequest("Error", "Failed to load the JPG image.", "OK", #REQICON_ERROR)
EndIf
EndIf
EndFunction
; Function to handle image drop
Function p_DropFunc(msg)
original_image = LoadBrush(Nil, msg.DropFiles[0])
If original_image
DisplayBrush(original_image, 0, 0)
DrawRectangle()
DebugPrint("JPG image loaded successfully via drop.")
Else
DebugPrint("Failed to load the image.")
EndIf
EndFunction
; Function to handle mouse events
Function p_MouseFunc(msg)
If msg.action = "OnLeftDown"
StartDragging(msg.x, msg.y)
ElseIf msg.action = "OnLeftUp"
StopDragging()
ElseIf msg.action = "OnMouseMove"
DragRectangle(msg.x, msg.y)
EndIf
EndFunction
; Define the menu
@MENU 1, {
{"File", {
{"Load Image", ID = "load"},
{"Exit", ID = "quit"}
}}
}
; Assign the menu to the window
@DISPLAY {Menu = 1}
; Function to handle menu events
Function p_MenuFunc(msg)
If msg.action = "OnMenuSelect"
Switch msg.item
Case "load"
LoadImage()
Case "quit"
End
EndSwitch
EndIf
EndFunction
; Install event handlers
InstallEventHandler({
OnDropFile = p_DropFunc,
OnMenuSelect = p_MenuFunc,
OnLeftDown = p_MouseFunc,
OnLeftUp = p_MouseFunc,
OnMouseMove = p_MouseFunc
})
; Display instructions
TextOut(28, 28, "Drop an image here or use File -> Load Image")
TextOut(28, 44, "Click and drag to move the rectangle")
; Main program loop
Repeat
WaitEvent
Forever