Add text to image

Windows specific forum
cecilcheah
Enthusiast
Enthusiast
Posts: 168
Joined: Wed Jun 04, 2003 8:44 am
Location: Switzerland

Add text to image

Post by cecilcheah »

Hi there

One more question today.

If a desktop image is captured in a clipboard, how do i add some text to it? For example i want to put some text in the center of the image, how do i do this?

Thanks

Cecil
Saboteur
Enthusiast
Enthusiast
Posts: 273
Joined: Fri Apr 25, 2003 7:09 pm
Location: (Madrid) Spain
Contact:

Post by Saboteur »

Look for 2D Drawing in help, there is an example. With StartDrawing() function and ImageOutput(), WindowOutput(), etc... you can draw where you want. :)
[:: PB Registered ::]

Win10 Intel core i5-3330 8GB RAM Nvidia GTX 1050Ti
cecilcheah
Enthusiast
Enthusiast
Posts: 168
Joined: Wed Jun 04, 2003 8:44 am
Location: Switzerland

Post by cecilcheah »

You are right. It has an example to show you how to draw text over an image in a window/gadget, but not in a clipboard .

How can i add some text to a captured image when the image is in the clipboard? I try with ImageOutput but it always give me an error.

Thanks

Cecil
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

I imagine at some time you wish to save the captured screen image to disk? why not add the text in the save function, e.g. write the image to disk then draw on it, all in one go?
--Kale

Image
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Code: Select all

If CreateImage(0, 320, 200)
  foo.l = GetClipboardData(#PB_Clipboard_Image)
  If foo
    If StartDrawing(ImageOutput())
      DrawImage(foo, 0, 0)
      DrawText("some crap")
      StopDrawing()
    EndIf

    SaveImage(0, "c:\blah.bmp")
  EndIf
EndIf
End
Edit: cleaned up, forgot code tags.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Try this (providing you already have an image in the clipboard and you know its width and height):

Code: Select all

MyImage = CreateImage(0, width, height)
ImageID = GetClipboardData(#PB_ClipboardImage)
StartDrawing(ImageOutput())
DrawImage(ImageID, 0, 0)
; your own drawing here
StopDrawing()
SetClipboardData(#PB_ClipboardImage, UseImage(0))
El_Choni
cecilcheah
Enthusiast
Enthusiast
Posts: 168
Joined: Wed Jun 04, 2003 8:44 am
Location: Switzerland

Post by cecilcheah »

Thanks

I see where my error is.

Question:

Your code

Code: Select all

ImageID = GetClipboardData(#PB_ClipboardImage) 
StartDrawing(ImageOutput()) 
I try with this:

image=pasteImage ' pasteImage is a procedere inside my programme like your GetClipboardData(#PB_ClipboardImage)
StartDrawing(ImageOutput())

Do i have to use ImageID instead of image to let PB register a new ID for the image? I was always getting a GPF yesterday doing this way.

Always i have a question:

Code: Select all

MyImage = CreateImage(0, width, height) 
ImageID = GetClipboardData(#PB_ClipboardImage) 
What is MyImage for? Is it the same as ImageID. I think this is where i have a problem in understanding PB. Can i use:

MyImage = CreateImage(0, width, height)
MyImage = GetClipboardData(#PB_ClipboardImage)

instead?


I will try your way later.

How long does it take you to be so good in PB and API?

Cecil
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

cecilcheah wrote:I try with this:

image=pasteImage ' pasteImage is a procedere inside my programme like
pasteImage the way you have written it here is not a procedure. Procedures have brackets after them. pasteImage in this case will be a variable which (probably) has a value of zero, which is why your code GPFs.
Do i have to use ImageID instead of image to let PB register a new ID for
No, the value returned by GetClipboardData is just a handle to the data, so you can get access to it later. You can give it any variable name you want.

Code: Select all

MyImage = CreateImage(0, width, height) 
ImageID = GetClipboardData(#PB_ClipboardImage) 
What is MyImage for? Is it the same as ImageID. I think this is where i
MyImage is just a handle to the image that is created. It has the same purpose as ImageID, but it is different (MyImage refers to the image created by CreateImage and ImageID refers to the image from the clipboard).
MyImage = CreateImage(0, width, height)
MyImage = GetClipboardData(#PB_ClipboardImage)
You can do that, but then MyImage will refer to the image from the clipbaord. You can still access the image you create in other ways, so it does not matter that you change the value like this. But it is quite needless to do so.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

Hi. MyImage and ImageID are both Bitmap handles:

Code: Select all

MyImage = CreateImage(0, width, height) ; create a new empty bitmap, retrieve handle
ImageID = GetClipboardData(#PB_ClipboardImage) ; get clipboard bitmap handle
StartDrawing(ImageOutput()) ; start drawing to the bitmap we created
DrawImage(ImageID, 0, 0) ; draw the image from clipboard
; your own drawing here 
StopDrawing() 
SetClipboardData(#PB_ClipboardImage, MyImage) ; we can use UseImage(0) instead, it's the same
El_Choni
Post Reply