Search code examples
c#winformswia

How to show an image obtained from a scanner using WIA in a picture box


I want to display an image retrieved from a scanner in a picturebox. So far I have been able to do it saving the image to a temporary file:

 private void btnScan_Click(object sender, EventArgs e)
 {
        var fn = System.IO.Path.GetTempFileName();
        System.IO.File.Delete(fn);
        var image = Scanner.Scan();
        image.SaveFile(fn);
        _ScannedImage = Bitmap.FromFile(fn);
        pictureBoxMain.Image = _ScannedImage;
 }

Is there a way to convert the image data in the ImageFile class avoiding to save it to disk?


Solution

  • You can convert the ImageFile to Image, and after that create a Bitmap object:

    var myImage = (Image)imageFile;
    Bitmap bmp = new Bitmap(myImage);
    

    Therefore you don't have to save (as you requested) in path.

    Please tell me if this works for you.

    See this post about converting from ImageFile to Image: Load a picturebox from a WIA ImageFile?