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.
Related
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 ?
I'm trying to make a Media Player where you can choose either to render a file from url or local disc. I had no problem making it to open and render url file
void LoadVideo_Click(object sender, RoutedEventArgs e)
{
LoadVideo.IsEnabled = false;
mediaElement.Source = new Uri(path, UriKind.Absolute);
With string path = "http://www.blablabla.com/movie.wmv"
The problem occurs when I'm trying to specify local disc file path(as "c:\movie.wmv" or #"c:\movie.wmv"). It simply doesn't work that way.
As far as I have read, you don't have direct access to files on your hard drive besides those which already are in the project directory. What I want to do is:
use Dialog Box to choose a file to open
save the path of file into string and transfer it to MediaElement.Source
Unfortunately, I don't have a clue how to do it. I would be grateful for any advices.
Here you go, this should do the trick:
OpenFileDialog fdlg = new OpenFileDialog(); //you need to use the OpenFileDialog, otherwise Silverlight will throw a tantrum ;)
fdlg.Filter = "MP4 Files|*.mp4|AVI files|*.avi"; //set a file selection filter
if (fdlg.ShowDialog() != true) //ShowDialog returns a bool? to indicate if the user clicked OK after picking a file
return;
var stream = fdlg.File.OpenRead(); //get the file stream
//Media is a MediaElement object in XAML
Media.SetSource(stream); //bread and butter
Media.Play(); //no idea what this does
Here's an extensive example on how to use the OpenFileDialog. As for the MediaElement, you can see in the code above all you needed was the SetSource() method (as opposed to the Source property).
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();
I'm working with an API that produces images in BMP or JPG format. The API method in question requires a path and filename. However, in some cases, I would like to simply put the image into the Clipboard.
I know I can save to a file, then open the file and copy the Stream's data to the Clipboard:
private void SaveSnapshot(string path)
{
var pathWasblank = path.IsNullOrBlank();
path = path.NullIfBlank() ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),"tempFile.jpg");
myApiInstance.ExportJPG(path, 100, true);
//to ensure the file has made it to the filesystem
while(!File.Exists(path)) Thread.Sleep(100);
if (pathWasblank) //it should be on the Clipboard and not in a file
{
using (var stream = File.OpenRead(path))
Clipboard.SetImage(Image.FromStream(stream));
File.Delete(path); //cleanup;
}
}
What I want to know is if there is a way to skip having to reopen, copy and then delete the file, and instead "save" directly to the Clipboard by specifying some file path that corresponds to the Clipboard. If not, no big deal; I have a working solution.
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