How to add a background image to a button in pygame gui? - user-interface

I am trying to add an image as a background of a button in pygame gui. I have the button created now and I just need to add the image. How can I add the image to the button?
This is my code
game1 = pygame.Rect(150 , 100, 200, 150) # creates a rect object
pygame.draw.rect(screen, [255, 100, 0], game1) # draw objects down here
it works fine

You're probably looking for pygame.Surface.blit (http://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit)
Load your background image and blit it wherever you want.
img = pygame.image.load('thisisanimage.png')
WhateverYourDisplayNameIs.blit(img,(x,y))
Just in case you're confused, it looks like you're using screen as your display surface.

Related

Hexapdf: trying to draw white box, but its not appearing

When I run my code, my white box does not appear. I need a white box to cover existing images, text, so I can add new text. If I change the color to background_color: [255,255,180], the box is a transparent yellow. However, I need a non-transparent white.
require 'hexapdf'
require 'pry'
doc = HexaPDF::Document.open('template.pdf')
pages = doc.pages
box = HexaPDF::Layout::Box.create(
width: 500, height: 500, content_box: true,
background_color: [255,255,255]
)
pages.each do |p|
canvas = p.canvas(type: :underlay)
box.draw(canvas, 20, 100)
end
doc.write("template_with_white_box.pdf")
You need to use canvas = p.canvas(type: :overlay) for this to work because the underlay canvas draws beneath the existing page whereas the overlay canvas draws above the existing page.

Creating a template image dynamically for OSX menu bar

I'm having trouble creating a template image for the OSX menu bar. As far as I can tell, it has to be a PDF image. To that end, I have:
var pageRect: CGRect = CGRectMake(0, 0, CGFloat(10), CGFloat(barHeight))
let pdfData: NSMutableData = CFDataCreateMutable(nil, 0)
let pdfConsumer = CGDataConsumerCreateWithCFData(pdfData as CFMutableDataRef)
let pdfContext = CGPDFContextCreate(pdfConsumer, &pageRect, nil)
Then I draw into the PDF:
CGPDFContextBeginPage(pdfContext, nil)
CGContextSetRGBFillColor (pdfContext, 1, 0, 0, 1)
CGContextFillRect (pdfContext, CGRectMake (0, 0, 200, 100 ))
CGPDFContextEndPage(pdfContext)
Then I try to create an NSImage:
let image = NSImage(data: pdfData)
And add it to the status item's image property:
button.image = image
However, this just isn't working. I have tried saving the image to disk and opening it, but get the message that the image is corrupted, so I suspect the error is in converting the pdfData into an NSImage, though I'm not totally confident of that. Anyway, any help would be appreciated.
A template image does not have to be a PDF. To make an instance of NSImage a template image, simply set its template property to true.
No matter the nature of the image (bitmap, PDF, whatever), the system will only make use of its alpha channel when it's a template image. The color channels are ignored.
The system will automatically mark an image loaded from a file as a template image if its filename-minus-extension ends with "Template". So, for example, fooTemplate.png or barTemplate.pdf.

How to place my logo on my map without white fields?

I have a problem with the my picturebox.
I want to place it on my map which i got in my program.
That works when i put my img in a Picturebox and then BringToFront();
i wanted to add a picture with the problem, but i just started at StackOverflow and doesnt have enough reputation yet... :(
Anyway my img got displayed but with the white fields around it. The img I use doenst have these white stuff around it.
How can i make my Picturebox transparent so that the white fields get removed.
LogoBox.Location = new Point(size.Width - 340, size.Height - 100);
LogoBox.Image = Properties.Resources.Troepoet;
LogoBox.Size = new System.Drawing.Size(250, 40);
LogoBox.SizeMode = PictureBoxSizeMode.StretchImage;
LogoBox.BackColor = Color.Transparent;
I tried to do it with only drawing a bitmap aswell but then i cant see any possibility to place it on the map. The map is a 'dominant' control.
Any help/suggestions?
Thx.
You don't need to put a picturebox on the map to draw your logo.
The easy way is to override the Map OnPainOverlays() and draw whatever you want (Logo, Text, any shape) on the map or another way if you don't want to inherit the GmapControl and work with the GMapControl by drag & drop it over a form to handle the Paint() Event:
Private Sub GMapControl1_Paint(sender As Object, e As PaintEventArgs) Handles GMapControl1.Paint
Dim logo As Image = image.FromFile("C:\transparentLogo.png")
e.Graphics.DrawImage(logo, GMapControl1.Width - logo.Width - 5, 5, logo.Width, logo.Height)
End Sub
Make sure you make image transparent and save it as PNG format.

HTML5 canvas - Rescale images on load?

Ok, so I'm doing an html5 canvas game, and I need to draw resized images all the time (it's all pixel-art). Unfortunately, doing the resizing on drawImage makes current browsers quite sluggish, so I'm trying to do the resizing on load, and then just draw the pre-resized image.
I've tried to draw the resized images to a hidden context and then do a ctx.getImageData, but then I'm stuck with a byte array and there's no way to convert to an image. I can do a putImageData to push it to the final context, but that's slow and I apparently lose the alpha channel.
Another option could be to pre-scale things in the server, but I'd like to avoid that if at all possible.
Any ideas?
There is a method on the canvas object (not the context) called toDataURL(string mimeType), this will convert the canvas contents to a base64-encoded binary string of an image. You can use this as the src attribute of any image element.
ctx.drawImage(originalImage, 0, 0, originalImage.width, originalImage.height, 0, 0, 200, 200);
var scaledImage = new Image();
scaledImage.onload = ...;
scaledImage.src = canvas.toDataURL("image/png");
Now you can draw your scaled image normaly.

How can I crop an image in Qt?

I load a PNG image in a QPixmap/QImage and I want to crop it. Is there a function that does that in Qt, or how should I do it otherwise?
You can use QPixmap::copy:
QRect rect(10, 20, 30, 40);
QPixmap original('image.png');
QPixmap cropped = original.copy(rect);
There is also QImage::copy:
QRect rect(10, 20, 30, 40);
QImage original('image.png');
QImage cropped = original.copy(rect);
Use QImage instead of QPixmap:
QImage image("initial_image.jpg");
QImage copy ;
copy = image.copy( 0, 0, 128, 128);
copy.save("cropped_image.jpg");
This code will save a file cropped to upper left corner 128x128px.
Since you use QPixmap, you can use its copy method and supply it with a QRect to perform the actual crop.
Just use of the QPixmap's copy() functions.
This text is result of reading the first comment on your quiestion:
Sometimes it is better to wrap around an image. That is to have an image that is part of another image or in other words points to a part of another image. This is way the wrapped image does not require additional memory, except for its header. You can display or save the wrapped image without worries. The downside is that the original image must remain valid until you use the wrapped image, also if you are drawing in the wrapped image it will affect the source.

Resources