C
C#10mo 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
leowest10mo 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 diouOP10mo ago
i can't just open it in the image gallery application, that is
leowest
leowest10mo 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 diouOP10mo ago
that was meant to be a question ahh i see
leowest
leowest10mo ago
you could use say Process to launch an application telling it to show the cat
Damsey diou
Damsey diouOP10mo ago
how do i do that though
leowest
leowest10mo 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 diouOP10mo ago
and where in the folder would i put image.png
ACE OF SPADES
ACE OF SPADES10mo ago
it depends on you where you locate your folder then change the path ProcessStartInfo("image.png"); <-- your image location path
Damsey diou
Damsey diouOP10mo ago
but i can't put a path, then it won't work between machines
leowest
leowest10mo ago
what are u trying to do?
Damsey diou
Damsey diouOP10mo 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
leowest10mo 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 diouOP10mo ago
no? i just need the game to work on different computers
leowest
leowest10mo 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 diouOP10mo ago
yeah but if you use a file path, moving the folder with the exe in it would stop it from working
leowest
leowest10mo 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 diouOP10mo 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
leowest10mo 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 diouOP10mo ago
yeah because i don't know how to do that?
leowest
leowest10mo ago
well then ask dont just say its not working
Damsey diou
Damsey diouOP10mo ago
well its not working thats correct
leowest
leowest10mo 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 diouOP10mo ago
yea
leowest
leowest10mo 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 diouOP10mo ago
it dissappeared..?
leowest
leowest10mo ago
what did like if u dont give me context I wont be able to help
Damsey diou
Damsey diouOP10mo ago
the image. this image file just dissappeared
Arch Leaders
Arch Leaders10mo 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 diouOP10mo ago
im starting to think this is a bad way to go about it i might just use ascii for efficiency
Arch Leaders
Arch Leaders10mo 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 diouOP10mo 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
leowest10mo 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 Leaders10mo ago
It does Explorer seems to care about the path seperator So ./ is invalid, it needs .\
Damsey diou
Damsey diouOP10mo ago
unrecognised escape sequence
leowest
leowest10mo 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 Leaders10mo ago
I would have agreed, but I just tried it and adding .\ and it opens explorer and ignores the args.
leowest
leowest10mo ago
@".\image.png" what args?
Arch Leaders
Arch Leaders10mo ago
The file path
Damsey diou
Damsey diouOP10mo ago
this also literally just opens documents
Arch Leaders
Arch Leaders10mo ago
The second one opens explorer in the default location (Documents)
No description
leowest
leowest10mo ago
just this Process.Start("explorer.exe", "image.png");
Arch Leaders
Arch Leaders10mo ago
In that case, .\image.jpg does not exist (or whatever your image name is)
leowest
leowest10mo ago
should open in the image app
Damsey diou
Damsey diouOP10mo ago
i named my image "image.png" for ease of access this also just opens documents
leowest
leowest10mo ago
can u provide your exact code
Arch Leaders
Arch Leaders10mo ago
I'm guessing the issue is you image is not being copied to the build folder.
Damsey diou
Damsey diouOP10mo ago
Process.Start("explorer.exe", "image.png"); lmao
leowest
leowest10mo ago
because I feel we are running different things and you're on win10?
Damsey diou
Damsey diouOP10mo ago
yeah
leowest
leowest10mo 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 Leaders10mo ago
I think he set the type to resource, and the image disappeared. That's what I read anyway.
leowest
leowest10mo 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 diouOP10mo ago
omds i already did it
Arch Leaders
Arch Leaders10mo ago
Can you send a screenshot of the proeprties panel?
leowest
leowest10mo ago
and the solution
Damsey diou
Damsey diouOP10mo ago
No description
Damsey diou
Damsey diouOP10mo ago
there you go
No description
Arch Leaders
Arch Leaders10mo ago
Set the build action to Content
Damsey diou
Damsey diouOP10mo ago
lets go ok it does work so now i understand that
Arch Leaders
Arch Leaders10mo ago
:thumbs_up:
Damsey diou
Damsey diouOP10mo ago
whats a better way to display an image without it being awful
Arch Leaders
Arch Leaders10mo ago
:AHAHAH:
leowest
leowest10mo 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 diouOP10mo ago
can i display an image in the console?
Arch Leaders
Arch Leaders10mo ago
Yeah, but it's a but of a pain
Damsey diou
Damsey diouOP10mo ago
we already got it to work
Arch Leaders
Arch Leaders10mo ago
I would advise making a WinFormsUI
Damsey diou
Damsey diouOP10mo 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 Leaders10mo ago
You could also make a popup with the image
leowest
leowest10mo ago
OH I see for some odd reason u had to set it to content weird mine is set to none
Arch Leaders
Arch Leaders10mo ago
I've always had to set it Content, not really sure why but it works
leowest
leowest10mo ago
well what u said earlier would make sense then ascii stuff
Damsey diou
Damsey diouOP10mo ago
yeah but thats not really ideal
Arch Leaders
Arch Leaders10mo ago
Yeah an ASCII image would fit the style better if that's your end goal.
Damsey diou
Damsey diouOP10mo ago
plus, can't effectively use colours if it's ascii
Arch Leaders
Arch Leaders10mo ago
Yeah if you want color there is another way Just give me a minute I've only done it once
leowest
leowest10mo ago
why not
No description
leowest
leowest10mo ago
console app
Arch Leaders
Arch Leaders10mo ago
Also is a popup a viable solution, or do you need it to stay in the console?
Damsey diou
Damsey diouOP10mo ago
im okay with a popup effectively is the keyword without, you know, changing the console foreground every pixel?
Arch Leaders
Arch Leaders10mo 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
leowest10mo 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
leowest10mo 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 Leaders10mo ago
That's still the same black/white issue though
Damsey diou
Damsey diouOP10mo ago
that would be thousands of lines
Arch Leaders
Arch Leaders10mo 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 diouOP10mo 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
leowest10mo 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 Leaders10mo 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
leowest10mo 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 diouOP10mo ago
but how i won't
Arch Leaders
Arch Leaders10mo 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 diouOP10mo ago
i know how to figure out the best solution
Damsey diou
Damsey diouOP10mo ago
can you tell what the following image is:
No description
Arch Leaders
Arch Leaders10mo ago
Not really, no
Damsey diou
Damsey diouOP10mo ago
so ascii probably isn't a good idea
Damsey diou
Damsey diouOP10mo ago
fyi, it was this image turned into ascii
No description
Arch Leaders
Arch Leaders10mo ago
Ah, yeah You need less detailed images for anything ASCII to work.
Damsey diou
Damsey diouOP10mo 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 Leaders10mo 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 diouOP10mo ago
it looks like chica, foxy and freddy from five nights at freddy's
No description
Damsey diou
Damsey diouOP10mo ago
its really not good how do
Arch Leaders
Arch Leaders10mo ago
Is this for windows only?
Damsey diou
Damsey diouOP10mo ago
yeah probably
Arch Leaders
Arch Leaders10mo 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 Leaders10mo ago
First you'll need to add <UseWPF>True</UseWPF> to your csproj file.
No description
Damsey diou
Damsey diouOP10mo ago
"put an image in that window" doesn't sound like directly into the console
leowest
leowest10mo ago
No description
Damsey diou
Damsey diouOP10mo ago
sorry, should've clarified that was what i was saying "how do" about
leowest
leowest10mo ago
Damsey diou
Damsey diouOP10mo ago
wym by keep buffering
Arch Leaders
Arch Leaders10mo ago
I gave you two options, I was just giving you the first one.
Damsey diou
Damsey diouOP10mo ago
yeah i realise i should've clarified i was asking about the second one
Arch Leaders
Arch Leaders10mo ago
Ah okay, the post leowest linked will work for that then.
Arch Leaders
Arch Leaders10mo ago
Got a little sidetracked and made a simple class to insert an image.
Arch Leaders
Arch Leaders10mo ago
(You'll need to change IMAGE_PATH cause it's hardcoded to my PC)
Damsey diou
Damsey diouOP10mo ago
damn, thats clean
Want results from more Discord servers?
Add your server