dcsimg
Results 1 to 18 of 18

Thread: Fastest way to load an image from a file without locking it.

  1. #1

    Thread Starter
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Fastest way to load an image from a file without locking it.

    I made a PDF loading utility in one of my apps. To make things faster I created a PDF thumbnail extractor. This dumps the file to disk where I read it back into an image.

    I tried Image.FromFile() and had great results in the speed department.
    The problem is Image.FromFile locks the file so it cannot be overwritten.

    I tried using a filestream (Code below) instead and it does not lock the file;
    however it slows down the process in a massive way.

    Code:
      Private Sub LoadFile(ImageFilePath As String)
        Dim fs As FileStream
        ' Specify a valid picture file path on your computer.
        fs = New System.IO.FileStream(ImageFilePath, FileMode.Open, FileAccess.Read)
        Pic1.Image = System.Drawing.Image.FromStream(fs)
        fs.Close()
      End Sub
    Is there someway to speed up the file read or not lock the file with FromFile?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Re: Fastest way to load an image from a file without locking it.

    I remember a thread a while back where JMcIlhinney proved that PictureBox.Load was the quickest and safest method.

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Fastest way to load an image from a file without locking it.


  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Fastest way to load an image from a file without locking it.

    Load it into a new bitmap, create second new bitmap cloning first. Dispose first bitmap... Simples.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Fastest way to load an image from a file without locking it.

    Code:
    Dim img1 As New Bitmap("path")
    Dim img2 As Bitmap = DirectCast(img1.Clone, Bitmap)
    img1.Dispose()

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Fastest way to load an image from a file without locking it.

    Quote Originally Posted by .paul. View Post
    Code:
    Dim img2 As Bitmap = DirectCast(img1.Clone, Bitmap)
    paul,
    The Clone method creates a new Bitmap, but it still is referencing the original native GDI image and any locks that it might have on a file.

    Your best bet is to use the Bimap constructor that take a source image. That overload draws the original to the new Bitmap thereby eliminating any links to the original.
    Code:
    Dim img2 As New Bitmap(img1)
    Reference:

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Fastest way to load an image from a file without locking it.

    The clone method doesn't keep the original locked. Once you dispose the original, you can modify the copy and save it back to the original file location. If you modify the original and attempt to save it back to the original file location you'll find it is locked.

  8. #8
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Fastest way to load an image from a file without locking it.

    Quote Originally Posted by .paul. View Post
    The clone method doesn't keep the original locked. Once you dispose the original, you can modify the copy and save it back to the original file location.
    You are entitled to your opinion, but I have never seen it work that way.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Fastest way to load an image from a file without locking it.

    Try it and see for yourself. Don't take my word for it...

  10. #10
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Fastest way to load an image from a file without locking it.

    Quote Originally Posted by .paul. View Post
    Try it and see for yourself. Don't take my word for it...
    I did try it several years ago and it failed. Tried it just now and it failed.
    Code:
    Dim img1 As New Bitmap("somepic.bmp")
    Dim img2 As Bitmap = CType(img1.Clone, Bitmap)
    img1.Dispose()
    img2.Save("somepic.bmp")

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,145

    Re: Fastest way to load an image from a file without locking it.

    ok it failed, but i'm sure i've used it before...

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,741

    Re: Fastest way to load an image from a file without locking it.

    If you don't specifically need an Image object but just want to display the image in a PictureBox then I would suggest calling the Load method of that PictureBox, as dday9 suggested that I suggested.

  13. #13

    Thread Starter
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Fastest way to load an image from a file without locking it.

    I am using a ghostscript wrapper to dump pdf files to a temporary png image file.

    This is the same png file for all reads to the picturebox.

    Picturebox.Load() just as Image.FromFile() locks the file so it cannot be written to when it is linked to the picturebox.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  14. #14
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Fastest way to load an image from a file without locking it.

    Gruff,

    Have you tried something like the following?
    Code:
    Public Shared Function GetMemoryBitmapFromFile(path As String) As Bitmap
    	Dim bm As Bitmap
    	Using img As Image = Image.FromFile(path)
    		bm = New Bitmap(img)
    	End Using
    	Return bm
    End Function

  15. #15

    Thread Starter
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Fastest way to load an image from a file without locking it.

    Thanks TnTinMN,

    That worked... However I see no speed difference between that and using a filestream.
    Oh well I guess the performance is what it is. No doubt Ghostscript has it's own bottleneck.

    Just discovered a thumbnail size issue that is more important.
    PDF file png output varies a lot more than I thought it would. Some are ledgible others are a dot.
    I'll start another thread on that issue.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  16. #16
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,741

    Re: Fastest way to load an image from a file without locking it.

    Quote Originally Posted by Gruff View Post
    Picturebox.Load() just as Image.FromFile() locks the file so it cannot be written to when it is linked to the picturebox.
    Are you sure about that? It was quite some time ago but I would have sworn that I tested that specifically and it was not the case. I can't right now but I'm going to have to test that again for myself.

  17. #17

    Thread Starter
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Fastest way to load an image from a file without locking it.

    As I did not capture times and my test app converts and load the file in one operation there could well be a difference in speed just not visible to the naked eye.

    Did a test and the lag time I am seeing is definitely due to the conversion not the load.

    I may have a look at Ghostript.NET instead of rolling my own GhostScript wrapper.
    It says it can return a thumbnail image object directly. This might be a better solution.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  18. #18
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Fastest way to load an image from a file without locking it.

    Copy the file as a temp and use the temp?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width