How to load user generated png images as textures (not xnb's)? - c#

The idea is to let users create their own textures, put them in a folder as images (and maybe set some properties in a separate text file) for the game to load and work accordingly.
Usually I have my own textures as images, they are processed by Visual Studio when compiling, and then the game uses XNB files. But how about end users that don't have VS installed?
UPD: The only safe option (that doesn't require manual resource disposal) seems to be replacing the original resource files in XNB format. For that, you can process your own PNG, WAV and other files with this tool from codeplex and put them in content folder of the game.

if you works in windows only... (not xbox and not windows phone)
you can use Texture2d.FromStream(File.OpenRead(path));
You have to realize that this way, you should call the texture dispose method when the texture is not needed to free resources.
Wehn you use the content manager, is the manager who call the method when the game ends.

Related

How can I search for all non-resource image (paths) on a drive?

I want to write a program that gathers all pictures that someone may have on their computer, while ignoring any resource files (i.e. a .jpg for a game icon, for a game in their Program Files, etc.)
What can I research/learn about for accomplishing this?
Is there some sort of meta-data that can be tied to user-generated pictures, etc?
I want to start with .jpg/.jpeg files at first, but eventually I'd like to include:
Any Camera files that were uploaded and saved to the computer.
Any picture that was downloaded from the internet and saved.
files from image creation/editing tools such as aseprite or photoshop and saved.
Any help is appreciated and any criticism is accepted.
Thank you in advance for your direction.
Edit: For a use case example, I'd like to be able to search through a drive for pictures that a user has, that they may not remember where they saved them.
There is no reliable way to know if a image is intended to be used as a resource. Programs can be stored anywhere on the drive, so using the path will not be reliable. And images from the internet may have arbitrary metadata.
My suggestion would be to find all images (note that this will take some time), and use some heuristic to guess what folders represent an image archive. Presumably most people would store photos in one or a few different folders. If all subfolders mostly contain image files, and no files like .exe or .dll files, it would be a good candidate. But you should probably ask the user to confirm the folder selection and allow for changes.
Note that things like cached internet images would probably not be included since they will be mixed in with other types of cached data.

C# MonoGame Help? (Content.Load<Texture2D>("Invader");)

I am Making a Space Invaders Game using Open GL in MonoGame and I am trying to load a texture that I have added to the Content folder (It is a PNG file called "Invader")
The code that I use is:
invader = Content.Load<Texture2D>("Invader");
However when I attempt to run it It says:
ContentLoadException was unhandled could not load Invader as a
non-content file!
I am trying to load a texture that I have added to the Content folder (It is a PNG file called "Invader")
invader = Content.Load("Invader");
Actually, you can load the PNG content that has been added to the Content folder directly like so:
invader = Content.Load<Texture2D>("Invader");
Note that the filename is case senitive on some platforms so be careful that it matches exactly. Also, make sure you've set the file to Content / Copy if newer in the Properties window.
The alternative is to compile your assets into optimized binary XNB files using the XNA Game Studio Content Pipeline or the MonoGame Content Pipeline. This will give you better performance but carries extra development overhead.
I should also mention that when rendering your sprites as raw PNG files you should use BlendState.NonPremultiplied in the call to SpriteBatch.Begin for best results. I've been doing it this way in my games for a while and I'm pretty happy with the results.
MonoGame does not fully implement the content manager. Typically, you build the content separately, and import the built content files into your project. Then you can load them as usual.
To build the content files, you can use an XNA or MonoGame content builder such as this one. If you prefer, you can use command lines as part of your project's build process so that content is built automatically.
Make sure your Build Action is set to Content for the .png in question. Do this by right clicking the file and selecting properties.

Working with BitMaps (PNG files) in XNA, System.drawing not available

I am sorry that this is a little bit ambiguous. I am having an issue using System.Drawing in XNA; from my research it is not available to XNA (since its part of the windows.dll?)
I want to create a sprite sheet analyzer which automatically dissembles a sprite sheet into its proper segmentation, number of frames, etc. for later playback. For this I need to grab the actual PNG file and it would be nice to have something that already has the functionality for working with images. Is there a class in XNA which provides similar functionality as System.drawing?
You can use System.Drawing in conjuction with XNA with no problem -- you just have to add a reference to it in your XNA project. However, System.Drawing does not support loading of .pngs, while XNA does.
The usual way to load images in XNA is to first add them to your content project (usually when you create an XNA project there is always a corresponding content project created). Add the saved .png to your content project and give it a unique name. Then in your code, load the image as a Texture2D:
Texture2D myTexture = Content.Load<Texture2D>("my image name");
Note the use of Content which is a ContentManager object that can be referenced from the Game object you are currently using for your XNA game.
Check this out for more information.
I found the answer. I can actually obtain the color data from the texture2D, allowing me to disassemble an image for analysis without using any extra libraries outside of XNA
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series2D/Texture_to_Colors.php

Handle lots of images in C# application

I'm trying to create a card game in C# and for this I have alot of images that I need to load. They're all jpg images and there are about 7000 of them.
I would like to make sure that if you download the game, the images will not be easily accessible, meaning that they should not just be JPG images in a sub folder of the application. So I thought about imbedding them in a DLL file.
But how do I do this? And how do I handle this efficiently? Is there a tecnique to this sort of thing, or is another method preferable?
I would like to make sure that [...] the images will not be easily accessible
First, you should ask yourself why you want to forbid this. If you just want to avoid that someone else manipulates the pictures, you can leave them in a bunch of subfolders as JPGs, just generate checksums for each file and check them at the time the program loads the pictures.
If you want to avoid reuse of the pictures, you can leave them in a bunch of subfolders, but not as JPGs. Encode them with for example with the standard AES algorithm. But beware, that won't prevent anyone else of making screenshots while you application is running, so you should consider if that's really worth the effort.
EDIT: if you want to embed the images because installation gets easier when you have just one big file to deploy instead of 7000 single files, then you may write a helper program for creating resource files programmatically. See this page from Microsoft, especially the part about .resource files, to learn how to utilize the ResourceWriter class for that purpose.
If you have 7000 image, you need a database. Microsoft SQL Server Compact 4.0 is an option. It's small and easy to use.
I'm assuming that this is a windows application
In order to Embed a Image to the assembly
1. Right click the Image file and Select properties
2. In the Properties Pane Set the BuildAction as Embeded resource
So this Image becomes a embeded resource when the application is compiled
Then you can access the Image from the assembly like:
global::[[MyNameSpace]].Properties.Resources.[[ImageName]]
for eg:this.pictureBox1.Image = global::[[MyNameSpace]].Properties.Resources.[[ImageName]]

C# Move video files into folders based on their video frame size.

I have a hard drives dedicated to videos, and I wanted to write a program that would move all my video files into folders based on their video playback size.
I was thinking about having it organized like this.
/HD/1080p/(FileName)/(fileName).ext
/HD/720p/(FileName)/(fileName).ext
(I know that not all video files are 1080 or 720p because of crop, but within +-20 to 30px.)
/SD/(FileName)/(fileName).ext //anything less then 720p
I know you are able to right click on a video file and go to properties then details and see the frame width and frame height, but I'm not sure you can view this information in C#.
I don't know where to start and some information would be awesome. like:
Moving files with c#, renaming them, Viewing file details (frame sizes, file type, name, lenght, etc.) I plan on making a DB on this information but as of right now I just want to move the files into the correct folders.
I have been doing this manually and it's very tedious and time consuming.
Any help would be awesome, Thanks,
Throdne
The best for getting file info properties is to use MediaInfo.dll. There is also c# wrapper available to collect all data you need from video file.
You can obtain media ifo from mediainfo.sourceforge.net
This is multiplatform and can be used on Mono and Linux as well on Windows.
I've put also some information about MediaInfo on following thread: https://stackoverflow.com/questions/9561490...
Your best bet is using something like DirectShow which will handle multiple video formats there is a com+ object you can attach to but on source forge there is a wrapper around the
API
Info on sourceforge
once you have that figured out you can then go
here to figure out how to move files around

Categories