Easiest way to display images

Adam D. Ruppe destructionator at gmail.com
Tue Feb 5 21:25:54 UTC 2019


On Tuesday, 5 February 2019 at 21:11:52 UTC, Murilo wrote:
> What would be the easiest way to simply display a jpg or png 
> image on the screen for a few seconds in Windows?

That's a little more complex than playing a sound, there's no one 
function to do it. You can in... oh about 50 lines, but I can't 
write them off the top of my head.

However, I do have a library that can do it in 5 lines.

---
import arsd.simpledisplay;
import arsd.image;

void main() {
	displayImage(Image.fromMemoryImage(loadImageFromFile("filename 
here")));
}
---

I realize that line is a bit silly looking, it loads a file, 
converts it to a displayable format, then pops up a window to 
display it. Any key pressed in the window will close it.

You might need something more complex, and the lib can do it. You 
might also want something less complex (that `arsd.image` 
actually loads 7 different modules for various image formats), 
but it isn't super hard to use anyway:

Download the code here:

https://github.com/adamdruppe/arsd

You can put all those files in an `arsd` folder next to your 
program, and `dmd -i yourfile.d` to compile it all at once.

Or if you want to load the files yourself, you can list them all. 
If you just need png and/or jpeg specifically, you can jus get 
jpeg.d and/or png.d, together with simpledisplay.d and color.d 
from the repo and call these functions:

http://dpldocs.info/experimental-docs/arsd.jpeg.readJpeg.html
http://dpldocs.info/experimental-docs/arsd.png.readPng.html


So the code looks like:
---
import arsd.simpledisplay;
import arsd.png;

void main() {
	displayImage(Image.fromMemoryImage(loadPng("filename here")));
}
---

or of course changing png to jpeg if you want that. (You can put 
that on an if based on the image filename - that's what the 
arsd.image library actually does).

Then compile:

dmd yourfile.d color.d simpledisplay.d png.d jpeg.d



But the first option with dmd -i is prolly the easiest.



If you need mroe control over the window, let me know, I will 
type that up too.

also take a look at the docs 
http://dpldocs.info/experimental-docs/arsd.simpledisplay.html



BTW I didn't mention this in your other thread because PlaySound 
is easier to use anyway, but I also have an audio module: 
http://dpldocs.info/experimental-docs/arsd.simpleaudio.html I'm 
just not 100% happy with it so I don't talk about it much.


More information about the Digitalmars-d-learn mailing list