is it possible to open an image file using c#?
for example, if i had a jpeg of a cat in the same folder as the c# file, could i open the photo as a window or something?
118 Replies
if you write a GUI then you can show the cat yes
otherwise best u could do is tell your console app to run an app that will open that image
aside from that all u would be able to do is read the file data
i can't just open it in the image gallery
application, that is
that is what I meant by
otherwise best u could do is tell your console app to run an app that will open that image
that was meant to be a question
ahh i see
you could use say Process to launch an application telling it to show the cat
how do i do that though
For example
this will try to open the image with the windows default app
and where in the folder would i put image.png
it depends on you where you locate your folder then change the path ProcessStartInfo("image.png"); <-- your image location path
but i can't put a path, then it won't work between machines
what are u trying to do?
i feel like i've already said this, im trying to open an image file
tbh whatever the easiest way is to do that
yes u did, but u just said between machines
so that changes things a bit
are u going to send the app with the image to some one?
no?
i just need the game to work on different computers
well what u normally do is u right click your project and publish it
and if u had added a image to your project
and marked that image to be copied in the properties
it will be published with the app
so it would be right next to the exe no matter were u ship it to
yeah but if you use a file path, moving the folder with the exe in it would stop it from working
what file path are u using
provide some context
so I can understand what ur doing and tell u what is wrong
because if I have an image the way I told u with a code like this
the one I posted above
it will look for a image in the same path the exe is at
it did not. i used to the code, put a file with the exe called image.png, and it did not display it
ok then show me your image properties in your solution
I will guess u did not do what I told u above about the image
and if u had added a image to your project and marked that image to be copied in the properties
yeah because i don't know how to do that?
well then ask
dont just say its not working
well its not working
thats correct
I did explain right how to do it, u skipped steps
it makes sense it doesnt work
u see the image in your project in the solution explorer?
yea
now select the image in the list
a properties window should open/show
there u see copy to output directory
u change it to copy if newer
it dissappeared..?
what did
like if u dont give me context I wont be able to help
the image. this image file just dissappeared
You can use a relative path.
.\image.jpg
will look in the working directory for image.jpg
Another (and probably better) approach would be using AppContext.BaseDirectory
to get the folder containing the exe and combing that with your image name.
Wait the entire conversation just updated
That might be a terribly outdated responceim starting to think this is a bad way to go about it
i might just use ascii for efficiency
This seems like a fine approach, it just needs tweaking.
Also learning this will be valuble, because including solution files is quite common.
i've used the following:
however it just opens file explorer to my documents
that opens the image on the default image viewer for windows
on windows 10
without the ./ althou i dont think it makes a diff
It does
Explorer seems to care about the path seperator
So
./
is invalid, it needs .\
unrecognised escape sequence
I was referring to having or not the .\ before the file name should make not different to it being open by the default app
because you're using it directly in side double quotes so u need to either make it verbatim or escpae it
I would have agreed, but I just tried it and adding
.\
and it opens explorer and ignores the args.@".\image.png"
what args?
The file path
this also literally just opens documents
The second one opens explorer in the default location (Documents)
just this Process.Start("explorer.exe", "image.png");
In that case,
.\image.jpg
does not exist (or whatever your image name is)should open in the image app
i named my image "image.png" for ease of access
this also just opens documents
can u provide your exact code
I'm guessing the issue is you image is not being copied to the build folder.
Process.Start("explorer.exe", "image.png");
lmao
because I feel we are running different things
and you're on win10?
yeah
possible he never told me if the did the image properties thing I told him to do twice
to set it to copy if newer
I think he set the type to resource, and the image disappeared. That's what I read anyway.
yeah indeed if the file does not exist it will open explorer
so he still havent set the image to copy if newer
omds i already did it
Can you send a screenshot of the proeprties panel?
and the solution
there you go
Set the build action to
Content
lets go
ok it does work
so now i understand that
:thumbs_up:
whats a better way to display an image
without it being awful
:AHAHAH:
can u try this
can i display an image in the console?
Yeah, but it's a but of a pain
we already got it to work
I would advise making a WinFormsUI
i am okay with pain
the game is a text adventue
so i want it to all be console where possible
You could also make a popup with the image
OH I see for some odd reason u had to set it to content weird mine is set to none
I've always had to set it Content, not really sure why but it works
well what u said earlier would make sense then ascii stuff
yeah but thats not really ideal
Yeah an ASCII image would fit the style better if that's your end goal.
plus, can't effectively use colours if it's ascii
Yeah if you want color there is another way
Just give me a minute I've only done it once
why not
console app
Also is a popup a viable solution, or do you need it to stay in the console?
im okay with a popup
effectively is the keyword
without, you know, changing the console foreground every pixel?
You could probably find a tool to convert a color image into color change tags, not sure if it's worth the bother though.
it would certainly not be easy and he would need to probably care about cursor position and where he is adding new stuff thou
there are also ways to convert image to ascii
not sure how accure it works thou
as an example https://www.codeproject.com/Articles/20435/Using-C-To-Generate-ASCII-Art-From-An-Image
CodeProject
Using C# To Generate ASCII Art From An Image
How to generate several types of ASCII art from an image file.
That's still the same black/white issue though
that would be thousands of lines
A popup is really easy, and by far the simplest solution, so I don't see why you shouldn't use it.
its alright, i think its probably best to just use regular black and white ascii
i said i was okay with a popup
its console anyting u do that u want to retain on screen u will have to write code taking that into account
Can you clarify? I can't quite comprehend that statement.
That's what I mean. If it works for your app, why not use it.
imagine u want to print an image to console on the top right corner, how do u do that
and then u want text showing in the same alignment on the left
but how
i won't
I was in the process of trying to explain that, but then we got sidetracked on using coloured ASCII
Depending on how you do it, the text buffer usually just copies over the image buffer
Anyway, @Damsey diou pick any solution and I'll do my best to explain a solution.
It's your app, and us arguing over the best solution isn't helping you learn.
i know how to figure out the best solution
can you tell what the following image is:
Not really, no
so ascii probably isn't a good idea
fyi, it was this image turned into ascii
Ah, yeah
You need less detailed images for anything ASCII to work.
precisely
thats why i tried to give it the edge, by whacking up the contrast on that image to max
i mean, maybe i could make it even simpler
I wouldn't really bother. ASCII typically only works with definitive objects.
Like an icon or something.
So I would say your only viable options are a popup, or displaying the image directly in the console.
it looks like chica, foxy and freddy from five nights at freddy's
its really not good
how do
Is this for windows only?
yeah
probably
In that case you can just use WPF to create a window and put the image in that window.
I'll provide some code examples in a second.
First you'll need to add
<UseWPF>True</UseWPF>
to your csproj
file."put an image in that window" doesn't sound like directly into the console
sorry, should've clarified that was what i was saying "how do" about
https://stackoverflow.com/questions/33538527/display-an-image-in-a-console-application
not exactly easy and u need to keep buffering but works
wym by keep buffering
I gave you two options, I was just giving you the first one.
yeah i realise i should've clarified i was asking about the second one
Ah okay, the post leowest linked will work for that then.
Got a little sidetracked and made a simple class to insert an image.
(You'll need to change IMAGE_PATH cause it's hardcoded to my PC)
damn, thats clean