c# - Image file copy, is being used by another process -


I am trying to create a user perfil edit window, this window has an image control - when I click on an image The file selected, it will show in this image control and will copy this file to my image folder, for the first time ok, but for the second time, it shows an error

"The process can not access the file." : \ 1.jpg 'because it can be used by any other process Yes. "

I think this is my image control using this file, so I do not know what I can do

  Private Zero Select_Click (Object Sender, RoutedEventArgs e) {OpenFileDialog od = New OpenFileDialog (); If (od.ShowDialog () == true) {string imageLocal = @ "C: /1.jpg"; File. Copy (od.fileName, imageLocal, true); Image1.Source = New bitmap image (new URI (image local)); }}    

If you want to load and display an image, and file the Keeping Responsible To work in the file system (such as reloading it or transferring it to another directory), the Uri constructor will not work because (as you say), the bitmap image class hangs on the file handle.

Instead, use a method like this ...

  Private static bitmap image biostream (file infos information) {// http: // social. Msdn.microsoft.com/Forums/en-US/wpf/ thread / dee7cb68-aca3-402b-b159-2de933f933f1 Try {if (info.Exists)} {// do so that the image file should be transferred to the file system BitmapImage Results = New Bitmap Image (); // Create new Bitmap stream stream stream = new memorystream (); // Create new memorystream bitmap bitmap = new bitmap (info.FullName); // Create a new bitmap (System.Drawing.Bitmap) from the existing image file (albumArtSource is set to its path name) bitmap. Save (stream, system, drawing. Imaging.image format.png); // Save the loaded bitmap to the memorystream - the PNG format was the only one that I tried so that there was no error (try JPG, BMP, MemoryBM) bitmap Dishes (); // Set the bitmap so that it releases the source image file results. BeginnInit (); // Start the initial results of the bitmap image. Stream source = stream; // Insert the streamsource of the bitmap image with the image result in the memorystream. EndInit (); // The initial result of the bitmap image is the return result; / / Finally, bitmapimage} Set the source of the WPF image component on the return tap; } Hold {return tap; }}   

This method takes a file infoto and gives a bitmap image that you can display and can move it to another directory or display it again can do. Public static bitmap image load bitmap image (string filename) {(var stream = new filestream (file name, FileMode.Open)) {var bitmapImage = new bitmap image (); BitmapImage.BeginInit (); BitmapImage.CacheOption = BitmapUp Download; BitmapImage.StreamSource = stream; BitmapImage.EndInit (); BitmapImage.Freeze (); Return bitmap image; }}

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -