C
C#4mo ago
Damsey diou

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
leowest
leowest4mo ago
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
Damsey diou
Damsey diou4mo ago
i can't just open it in the image gallery application, that is
leowest
leowest4mo ago
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
Damsey diou
Damsey diou4mo ago
that was meant to be a question ahh i see
leowest
leowest4mo ago
you could use say Process to launch an application telling it to show the cat
Damsey diou
Damsey diou4mo ago
how do i do that though
leowest
leowest4mo ago
For example
Process.Start("explorer.exe", "image.png");
Process.Start("explorer.exe", "image.png");
this will try to open the image with the windows default app
Damsey diou
Damsey diou4mo ago
and where in the folder would i put image.png
ACE OF SPADES
ACE OF SPADES4mo ago
it depends on you where you locate your folder then change the path ProcessStartInfo("image.png"); <-- your image location path
Damsey diou
Damsey diou4mo ago
but i can't put a path, then it won't work between machines
leowest
leowest4mo ago
what are u trying to do?
Damsey diou
Damsey diou4mo ago
i feel like i've already said this, im trying to open an image file tbh whatever the easiest way is to do that
leowest
leowest4mo ago
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?
Damsey diou
Damsey diou4mo ago
no? i just need the game to work on different computers
leowest
leowest4mo ago
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
Damsey diou
Damsey diou4mo ago
yeah but if you use a file path, moving the folder with the exe in it would stop it from working
leowest
leowest4mo ago
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
Damsey diou
Damsey diou4mo ago
it did not. i used to the code, put a file with the exe called image.png, and it did not display it
leowest
leowest4mo ago
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
Damsey diou
Damsey diou4mo ago
yeah because i don't know how to do that?
leowest
leowest4mo ago
well then ask dont just say its not working
Damsey diou
Damsey diou4mo ago
well its not working thats correct
leowest
leowest4mo ago
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?
Damsey diou
Damsey diou4mo ago
yea
leowest
leowest4mo ago
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
Damsey diou
Damsey diou4mo ago
it dissappeared..?
leowest
leowest4mo ago
what did like if u dont give me context I wont be able to help
Damsey diou
Damsey diou4mo ago
the image. this image file just dissappeared
Arch Leaders
Arch Leaders4mo ago
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 responce
Damsey diou
Damsey diou4mo ago
im starting to think this is a bad way to go about it i might just use ascii for efficiency
Arch Leaders
Arch Leaders4mo ago
This seems like a fine approach, it just needs tweaking. Also learning this will be valuble, because including solution files is quite common.
Damsey diou
Damsey diou4mo ago
i've used the following:
Process.Start("explorer.exe", "./image.png");
Process.Start("explorer.exe", "./image.png");
however it just opens file explorer to my documents
leowest
leowest4mo ago
that opens the image on the default image viewer for windows on windows 10 without the ./ althou i dont think it makes a diff
Arch Leaders
Arch Leaders4mo ago
It does Explorer seems to care about the path seperator So ./ is invalid, it needs .\
Damsey diou
Damsey diou4mo ago
unrecognised escape sequence
leowest
leowest4mo ago
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
Arch Leaders
Arch Leaders4mo ago
I would have agreed, but I just tried it and adding .\ and it opens explorer and ignores the args.
leowest
leowest4mo ago
@".\image.png" what args?
Arch Leaders
Arch Leaders4mo ago
The file path
Damsey diou
Damsey diou4mo ago
this also literally just opens documents
Arch Leaders
Arch Leaders4mo ago
The second one opens explorer in the default location (Documents)
No description
leowest
leowest4mo ago
just this Process.Start("explorer.exe", "image.png");
Arch Leaders
Arch Leaders4mo ago
In that case, .\image.jpg does not exist (or whatever your image name is)
leowest
leowest4mo ago
should open in the image app
Damsey diou
Damsey diou4mo ago
i named my image "image.png" for ease of access this also just opens documents
leowest
leowest4mo ago
can u provide your exact code
Arch Leaders
Arch Leaders4mo ago
I'm guessing the issue is you image is not being copied to the build folder.
Damsey diou
Damsey diou4mo ago
Process.Start("explorer.exe", "image.png"); lmao
leowest
leowest4mo ago
because I feel we are running different things and you're on win10?
Damsey diou
Damsey diou4mo ago
yeah
leowest
leowest4mo ago
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
Arch Leaders
Arch Leaders4mo ago
I think he set the type to resource, and the image disappeared. That's what I read anyway.
leowest
leowest4mo ago
yeah indeed if the file does not exist it will open explorer so he still havent set the image to copy if newer
Damsey diou
Damsey diou4mo ago
omds i already did it
Arch Leaders
Arch Leaders4mo ago
Can you send a screenshot of the proeprties panel?
leowest
leowest4mo ago
and the solution
Damsey diou
Damsey diou4mo ago
No description
Damsey diou
Damsey diou4mo ago
there you go
No description
Arch Leaders
Arch Leaders4mo ago
Set the build action to Content
Damsey diou
Damsey diou4mo ago
lets go ok it does work so now i understand that
Arch Leaders
Arch Leaders4mo ago
:thumbs_up:
Damsey diou
Damsey diou4mo ago
whats a better way to display an image without it being awful
Arch Leaders
Arch Leaders4mo ago
:AHAHAH:
leowest
leowest4mo ago
var image = "image.png";
if (!File.Exists(image))
{
Console.WriteLine("Image does not exists... press any key to terminate...");
Console.ReadKey();
return;
}
Process.Start("explorer.exe", "image.png");
var image = "image.png";
if (!File.Exists(image))
{
Console.WriteLine("Image does not exists... press any key to terminate...");
Console.ReadKey();
return;
}
Process.Start("explorer.exe", "image.png");
can u try this
Damsey diou
Damsey diou4mo ago
can i display an image in the console?
Arch Leaders
Arch Leaders4mo ago
Yeah, but it's a but of a pain
Damsey diou
Damsey diou4mo ago
we already got it to work
Arch Leaders
Arch Leaders4mo ago
I would advise making a WinFormsUI
Damsey diou
Damsey diou4mo ago
i am okay with pain the game is a text adventue so i want it to all be console where possible
Arch Leaders
Arch Leaders4mo ago
You could also make a popup with the image
leowest
leowest4mo ago
OH I see for some odd reason u had to set it to content weird mine is set to none
Arch Leaders
Arch Leaders4mo ago
I've always had to set it Content, not really sure why but it works
leowest
leowest4mo ago
well what u said earlier would make sense then ascii stuff
Damsey diou
Damsey diou4mo ago
yeah but thats not really ideal
Arch Leaders
Arch Leaders4mo ago
Yeah an ASCII image would fit the style better if that's your end goal.
Damsey diou
Damsey diou4mo ago
plus, can't effectively use colours if it's ascii
Arch Leaders
Arch Leaders4mo ago
Yeah if you want color there is another way Just give me a minute I've only done it once
leowest
leowest4mo ago
why not
No description
leowest
leowest4mo ago
console app
Arch Leaders
Arch Leaders4mo ago
Also is a popup a viable solution, or do you need it to stay in the console?
Damsey diou
Damsey diou4mo ago
im okay with a popup effectively is the keyword without, you know, changing the console foreground every pixel?
Arch Leaders
Arch Leaders4mo ago
You could probably find a tool to convert a color image into color change tags, not sure if it's worth the bother though.
leowest
leowest4mo ago
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
leowest
leowest4mo ago
CodeProject
Using C# To Generate ASCII Art From An Image
How to generate several types of ASCII art from an image file.
Arch Leaders
Arch Leaders4mo ago
That's still the same black/white issue though
Damsey diou
Damsey diou4mo ago
that would be thousands of lines
Arch Leaders
Arch Leaders4mo ago
A popup is really easy, and by far the simplest solution, so I don't see why you shouldn't use it.
Damsey diou
Damsey diou4mo ago
its alright, i think its probably best to just use regular black and white ascii i said i was okay with a popup
leowest
leowest4mo ago
its console anyting u do that u want to retain on screen u will have to write code taking that into account
Arch Leaders
Arch Leaders4mo ago
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.
leowest
leowest4mo ago
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
Damsey diou
Damsey diou4mo ago
but how i won't
Arch Leaders
Arch Leaders4mo ago
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.
Damsey diou
Damsey diou4mo ago
i know how to figure out the best solution
Damsey diou
Damsey diou4mo ago
can you tell what the following image is:
No description
Arch Leaders
Arch Leaders4mo ago
Not really, no
Damsey diou
Damsey diou4mo ago
so ascii probably isn't a good idea
Damsey diou
Damsey diou4mo ago
fyi, it was this image turned into ascii
No description
Arch Leaders
Arch Leaders4mo ago
Ah, yeah You need less detailed images for anything ASCII to work.
Damsey diou
Damsey diou4mo ago
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
Arch Leaders
Arch Leaders4mo ago
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.
Damsey diou
Damsey diou4mo ago
it looks like chica, foxy and freddy from five nights at freddy's
No description
Damsey diou
Damsey diou4mo ago
its really not good how do
Arch Leaders
Arch Leaders4mo ago
Is this for windows only?
Damsey diou
Damsey diou4mo ago
yeah probably
Arch Leaders
Arch Leaders4mo ago
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.
Arch Leaders
Arch Leaders4mo ago
First you'll need to add <UseWPF>True</UseWPF> to your csproj file.
No description
Damsey diou
Damsey diou4mo ago
"put an image in that window" doesn't sound like directly into the console
leowest
leowest4mo ago
No description
Damsey diou
Damsey diou4mo ago
sorry, should've clarified that was what i was saying "how do" about
leowest
leowest4mo ago
Damsey diou
Damsey diou4mo ago
wym by keep buffering
Arch Leaders
Arch Leaders4mo ago
I gave you two options, I was just giving you the first one.
Damsey diou
Damsey diou4mo ago
yeah i realise i should've clarified i was asking about the second one
Arch Leaders
Arch Leaders4mo ago
Ah okay, the post leowest linked will work for that then.
Arch Leaders
Arch Leaders4mo ago
Got a little sidetracked and made a simple class to insert an image.
Arch Leaders
Arch Leaders4mo ago
(You'll need to change IMAGE_PATH cause it's hardcoded to my PC)
Damsey diou
Damsey diou4mo ago
damn, thats clean