How do I load an image at run time? - c#

Hello guys I've tried to call an image at run time in XNA but it gives me an error "file not found" I've given the full path but it keeps returning a error. All I want is load a single image at some point the image does not exist when the game is executed (hard to explain). So I wanted to load this image generated by the game process is it possible?
if (File.Exists(FILE))//Checks if the file exist
ImageTexture = this.Content.Load<Texture2D>(#"C:\FullPath");

A: What is "C:\FullPath"? It's nothing. I very much doubt you have a file there.
B: XNA requires you load a local file if you're going to use Content.Load - it must be in the GamePath/Content folder. EG: GamePath/Content/MySprite.xnb
C: If you want to load a random image, you must use Texture2D.FromStream, like so:
System.IO.FileStream mystream = new System.IO.FileStream("C:/MyFile.png", System.IO.FileAccess.Read);
Image = Texture2D.FromStream(GraphicsDevice, mystream);
mystream.Dispose();

Related

Save Bitmap to disk and release saved file Rendered from DevDept Eyeshot

In a WPF application, I need to save a Bitmap to disk and than after some changes I try to delete the saved file and save again the modified image.
The problem is once saved, the bitmap doesn't release the file and throws an exception when I try to delete it.
//Gets a bitmap from DevDept.EyeShot model via Scene.RenderToBitmap() and save it to disk
using (var bmpRight = Scene.RenderToBitmap(new System.Drawing.Size(100, 100)))
{
bmpRight.Save(rightPath.Replace(".stl", ".jpg"));
}
Than the user modifies the 3D model and tries to save again with the same name. (Because it is associated to an appointment).
When I try to delete it
if (System.IO.File.Exists(rightPath.Replace(".stl", ".jpg")))
System.IO.File.Delete(rightPath.Replace(".stl", ".jpg"));
I throws an error :
The process cannot access the file because it is in use by another process.
If I do the same thing without Scene2.RenderToBitmap() method of the 3D plateforme, it doesn't produce any error. But, how can it be ? There is no relation between the physical file and the plateforme!
Any idea pleae ?

Difference between #"pack://application:,,,[...] and IO.Directory.GetCurrentDirectory()?

I've noticed that when I add a line of <Window.Background></Window.Background> in the XAML file or in the C# code this.Background = new ImageBrush(new BitmapImage(new uri([...])); if I put in the "Uri" this: new Uri(#"pack://application:,,,/Myapp;component/image.jpg") I get an error when I try to compile it which says: "Could not locate resource "image.jpg" "
But if I change this to new Uri(System.IO.Directory.GetCurrentDirectory()+"\\image.jpg"); it never gives me an error. Why???? What is the difference between both methods? I am very comfortable with Directory but why the compiler doesn't show an error "Locating" the resource with IO.Directory? What's the difference?
The questions may sound too noob, but I don't understand why it gives an error in #"pack://application[...] and not with GetCurrentDirectory()
A pack:// Uri, will search embedded resources for the image. The resources are inside the DLL/EXE, rather than deployed to the same folder. To use an image as a WPF Resource you need to set the Build Action on the image to Resource.
Using the Directory approach simply searches the physical file system for the image.
More info here on WPF Pack Uri's.

PNG and jpg images not appearing in C# application

I have a problem with displaying certain images in my application using C#. I am using the Image class to specify the location and the BitmapImage to specify the source. The UriSource is relative and I just specify the name. It worked for some images, but for others, the image simply does not appear. My image instance is 35x35 big and another is 100x100 big (pixels).
Anyone knows why this might be occurring and how to fix it?
Thanks.
Here's the code I used:
Image removeImage = new Image();
removeImage.HorizontalAlignment = HorizontalAlignment.Left;
removeImage.VerticalAlignment = VerticalAlignment.Top;
removeImage.Margin = new Thickness(490, 10, 0, 0);
removeImage.Width = 35;
removeImage.Height = 35;
BitmapImage source = new BitmapImage();
source.BeginInit();
source.UriSource = new Uri("delete.png", UriKind.RelativeOrAbsolute);
source.EndInit();
removeImage.Source = source;
removeImage.Stretch = Stretch.None;
removeImage.Visibility = Visibility.Visible;
removeImage.MouseDown += new MouseButtonEventHandler(removeImage_MouseDown);
Not sure about the location of image files. If images are in your current project folder then you have to set Copy To Output Directory=Copy Always property of image file from Properties Windows.
The best way that I know of to diagnose a problem like that (assuming a quick peer review of the code gets you nowhere), is to use ProcessMonitor: http://technet.microsoft.com/en-us/sysinternals/bb896645
You can use this tool to monitor all of the file activity on your machine (make sure to use the include/exclude filters to limit the noise).
It's very likely that the reason that the images are not showing up is because your application is looking for them in the wrong place (either they didn't get copied, or the relative path is off).
ProcessMonitor will log every attempt that Windows makes to access your .jpg (whether it fails or succeeds). If you search for your file name in the log, you should find it, probably along with an error message, and the full path that Windows was using to open the file.
The most common results I see are
Path that was actually being used was different from the path you needed.
The path was correct, but your files weren't there (build/copy/install problem)
The path was correct, but your web app did not have permissions to read the file.
In all those cases, ProcessMonitor will show you what happened.

c# ASP.NET How do i delete a file that is "in use" by another process?

I'm loading a file:
System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath);
Now I would like to save the image:
img.Save(SavePath);
This works.. Unless FilePath == SavePath, then it decides to give me the error:
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
So I tried to delete the file, right after opening it:
System.Drawing.Image img = System.Drawing.Image.FromFile(FilePath);
File.Delete(FilePath);
And it gives me the error:
System.IO.IOException: The process cannot access the file 'filename.jpg' because it is being used by another process.
So...
How can I modify an existing file, that's "in use" when its not in use by anyone?
The image will remain locked until it is disposed (See here).
The file remains locked until the Image is disposed.
You'll have to save the image somewhere else (or copy its contents out) and then dispose the opened image via using a using clause.
Example:
using(Image image1 = Image.FromFile("c:\\test.jpg"))
{
image1.Save("c:\\test2.jpg");
}
System.IO.File.Delete("c:\\test.jpg");
System.IO.File.Move("c:\\test2.jpg", "c:\\test.jpg");
you can either use Memory stream or put it in byte[] array
http://www.vcskicks.com/image-to-byte.php

Overwriting an image file (bitmap)

Hello I am trying to save a bitmap image in a basic image editor program.
Here's the code:
// then save it
ImageBoxInApp.Image.Save(filename);
[EDIT] And I am opening the image with this
openFileDialog1.Title = "Select an Image";
openFileDialog1.Filter = "All Files|*.*|Windows Bitmaps|*.bmp|JPEG Files|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog1.FileName;
Image img = System.Drawing.Image.FromFile(filename);
So when I try this I receive a Generic GDI+ error. So I found a solution to this and it looks like this:
// Delete existing file first
System.IO.File.Delete(filename);
// then save it
ImageBoxInApp.Image.Save(filename);
But when I try to do this I receive another error saying that the file I am deleting is currently open. This is because that is the file that I am trying to edit.
How do I "close" the file without actually closing the application? Or is there an alternative solution?
Thanks!
Its hard to say without seeing some context around how your loading the file, but here are a couple of suggestions:
Before saving, copy the existing image into a new Image, close the original, perform the delete, and save the copy.
Save the file to a random filename, delete the original, rename the randomly named file to the original name.
Load the file into a memory stream and use the in memory copy to initialize the image.
I'd personally go with option #3 for most cases. Be sure to dispose of the image when you've finished using it - it is best if you can wrap it in a using ( ) block.

Categories

Resources