title: Unlocking image from PictureBox
body: <p>I'm currently developing an application to help scanning and showing images here at my work.</p>
  
  <p>My application is build with multiple forms, the most important forms here is my <code>mainForm</code> to show statistics about the current scanning and a menustrip with different functinalities. I also have <code>ImageViewerForm</code> with a <code>PictureBox</code> which shows on the secondary monitor to view the current scanned image.</p>
  
  <p>I'm using a <code>Timer</code> to poll the folder where the images are scanned to. When a new image has been scanned and the image is unlocked, i'll grap it into a <code>FileStream</code> and show it in the <code>PictureBox</code>, see below:</p>
  
  <pre><code>public static void SetPicture(string filename, PictureBox pb)
  {
      try
      {
          Image currentImage;
  
          //currentImage = ImageFast.FromFile(filename);
          using (FileStream fsImage = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
          {
              currentImage = ScaleImage(Image.FromStream(fsImage), new Size(pb.Width, pb.Height));
  
              if (pb.InvokeRequired)
              {
                  pb.Invoke(new MethodInvoker(
                  delegate()
                  {
                      pb.Image = currentImage;
                  }));
              }
              else
              {
                  pb.Image = currentImage;
              }
          }
      }
      catch (Exception imageEx)
      {
          throw new ExceptionHandler("Error when showing image", imageEx);
      }
  }
  
  public static Image ScaleImage(Image imgToResize, Size size)
  {
      int sourceWidth = imgToResize.Width;
      int sourceHeight = imgToResize.Height;
  
      float nPercent = 0;
      float nPercentW = 0;
      float nPercentH = 0;
  
      nPercentW = ((float)size.Width / (float)sourceWidth);
      nPercentH = ((float)size.Height / (float)sourceHeight);
  
      if (nPercentH &lt; nPercentW)
          nPercent = nPercentH;
      else
          nPercent = nPercentW;
  
      int destWidth = (int)(sourceWidth * nPercent);
      int destHeight = (int)(sourceHeight * nPercent);
  
      Bitmap b = new Bitmap(destWidth, destHeight);
  
      using (Graphics g = Graphics.FromImage(b))
      {
          g.InterpolationMode = InterpolationMode.HighQualityBicubic;
          g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
      }
  
      return b;
  }
  </code></pre>
  
  <p>This way the image shown in the <code>PictureBox</code> shouldn't be locked, but it is.
  The problem is that the scanned image might have to be rescanned, and if I do that i'll get a sharing violation error when trying to overwrite the image file from the scanning software.</p>
  
  <p>Anyone who's got an answer to what I can do?</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Thanks to @SPFiredrake I've got a solution to create a temp file to show in the PictureBox, leaving the original image unlocked.</p>
  
  <pre><code>public static void SetPicture(string filename, PictureBox pb)
  {
      try
      {
          Image currentImage;
  
          //currentImage = ImageFast.FromFile(filename);
          using (FileStream fsImage = new FileStream(CreateTempFile(filename), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
          {
              currentImage = ScaleImage(Image.FromStream(fsImage), new Size(pb.Width, pb.Height));
  
              if (pb.InvokeRequired)
              {
                  pb.Invoke(new MethodInvoker(
                  delegate()
                  {
                      pb.Image = currentImage;
                  }));
              }
              else
              {
                  pb.Image = currentImage;
              }
          }
      }
      catch (Exception imageEx)
      {
          throw new ExceptionHandler("Error when showing image", imageEx);
      }
  }
  
  public static string CreateTempFile(string fileName)
  {
      if (string.IsNullOrEmpty(fileName))
          throw new ArgumentNullException("fileName");
      if (!File.Exists(fileName))
          throw new ArgumentException("Specified file must exist!", "fileName");
      string tempFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(fileName));
      File.Copy(fileName, tempFile);
  
      Log.New("Temp file created: " + tempFile);
  
      return tempFile;
  }
  </code></pre>