Results 1 to 13 of 13

Thread: cant save the image on the same file that I load from

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Question cant save the image on the same file that I load from

    ehehehe why is it like this:
    Dim img As Image = Image.FromFile("c:\funny.jpg")
    img.Save("C:\funny.jpg")

    this would give me an error. It works if I try to save it on some other file. It seems like you can't save the image on the same file that you load it from (well unless you clone the image, dispose it, and then try to save the cloned image. but thta's a lot of overhead/inefficient work)

    any way to get around this?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: cant save the image on the same file that I load from

    Originally posted by MrPolite
    ehehehe why is it like this:
    Dim img As Image = Image.FromFile("c:\funny.jpg")
    img.Save("C:\funny.jpg")

    this would give me an error. It works if I try to save it on some other file. It seems like you can't save the image on the same file that you load it from (well unless you clone the image, dispose it, and then try to save the cloned image. but thta's a lot of overhead/inefficient work)

    any way to get around this?
    nope, sounds like I cant even clone it. it gives an error on the last line
    VB Code:
    1. Dim path As String = "c:\funny.jpg"
    2.         Dim fs As New IO.FileStream(path, IO.FileMode.Open)
    3.         Dim img As Image = Image.FromStream(fs)
    4.         Dim img2 As Image = DirectCast(img.Clone, Image)
    5.         fs.Flush()
    6.         fs.Close()
    7.         img.Dispose()
    8.  
    9.         img2.Save(path)
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    bump
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you could do something like this ...
    VB Code:
    1. [color=blue]Dim[/color] fs [color=blue]As New[/color] IO.FileStream("C:\tester.bmp", IO.FileMode.OpenOrCreate)
    2.             [color=blue]Dim[/color] img [color=blue]As[/color] Image = Image.FromStream(fs)
    3.             fs.Close()
    4.             IO.File.Delete("C:\tester.bmp") [color=green]'/// delete the exsisting image.[/color]
    5.             img.Save("C:\tester.bmp")
    Or if you also want to modify the graphics...
    VB Code:
    1. [color=blue]Dim[/color] fs [color=blue]As New[/color] IO.FileStream("C:\tester.bmp", IO.FileMode.OpenOrCreate)
    2.             [color=blue]Dim[/color] img [color=blue]As[/color] Image = Image.FromStream(fs)
    3.             fs.Close()
    4.             IO.File.Delete("C:\tester.bmp") [color=green]'/// delete the exsisting image.[/color]
    5.             [color=blue]Dim[/color] grphics [color=blue]As[/color] Graphics = Graphics.FromImage(img)
    6.             grphics.DrawString("some stuff here", [color=blue]New[/color] Font("Tahoma", 10, FontStyle.Bold), [color=blue]New[/color] SolidBrush(Color.Red), 10, 10)
    7.             grphics.Save()
    8.             grphics.Dispose()
    9.             img.Save("C:\tester.bmp")
    hope it helps
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    or without even needing to delete the exsisting file ...
    VB Code:
    1. [color=blue]Dim[/color] img [color=blue]As[/color] Image = Image.FromFile("C:\tester.bmp")
    2.             [color=blue]Dim[/color] bmp [color=blue]As[/color] Bitmap = img
    3.             bmp.Save("C:\tester.bmp")
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by dynamic_sysop
    or without even needing to delete the exsisting file ...
    VB Code:
    1. [color=blue]Dim[/color] img [color=blue]As[/color] Image = Image.FromFile("C:\tester.bmp")
    2.             [color=blue]Dim[/color] bmp [color=blue]As[/color] Bitmap = img
    3.             bmp.Save("C:\tester.bmp")
    neither one works

    this is what I tried, I tried the one above in the quatations also:
    VB Code:
    1. Dim fs As New FileStream("C:\colormatrix.jpg", FileMode.Open)
    2.         Dim img As Image = Image.FromStream(fs)
    3.         fs.Close()
    4.         File.Delete("C:\colormatrix.jpg")
    5.         img.Save("C:\colormatrix.jpg")
    gives the same error on the last line, after it has deleted the file only creates a 0kb file

    this is stupid! I dont even want to delete teh file first, why the heck does this happen? it's nonsense
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    bump
    any genius can come up with a magical solution? the framework seems to be screwed up in this case
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  8. #8
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    FromFile opens the file, but never closes it. I researched this a while back, MS never declared it a bug, but by design. I personally never understood why anyone who open a file and not want to close it. It closes the file when you set the Image=Nothing (and then at some point when GC does collection) http://msdn.microsoft.com/library/de...filetopic1.asp

    SOLUTION: Don't use it. Use FromStream instead. Load into a MemoryStream and grab the image from there.
    Last edited by nemaroller; Nov 2nd, 2003 at 07:54 PM.

  9. #9

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by nemaroller
    FromFile opens the file, but never closes it. I researched this a while back, MS never declared it a bug, but by design. I personally never understood why anyone who open a file and not want to close it. It closes the file when you set the Image=Nothing (and then at some point when GC does collection) http://msdn.microsoft.com/library/de...filetopic1.asp

    SOLUTION: Don't use it. Use FromStream instead. Load into a MemoryStream and grab the image from there.
    nemaroller ,I'v tried the fromStream method, but same problem.

    This is what I want to do: load an image, save it on the same file WITHOUT disposing the image...
    I am writing a little image-editing program and I want to be able to save changes to the image. Now when the user clicks on the save button, it has to save the image on the same file, and I get that nasty error this way.

    I'm doing something like this right now:
    dim fs as new filestream(imagePath)
    dim img as image = image.fromFile(fs)
    fs.close

    file.delete imagePath
    img.save (imagePath)

    the last line gives the error. I've tried to clone img, dispose it, and then save the clone instead, but teh same thing happens. Messing with garbage collector doesnt seem to help either. I am clueless.
    I'd appreciate anyhelp. at least some of you guys have had the same problem before and know what I'm talking about

    edit: I've never tried memoryStreams though. any examples?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  10. #10
    Member
    Join Date
    May 2003
    Posts
    58
    Try this, save the image to temp file then replace the original with temp one, finally delete the temp file.


    VB Code:
    1. Dim s As FileStream = File.Open("c:\image001.jpg", FileMode.Open)
    2.         Dim img As Image = Image.FromStream(s)
    3.         img.Save("c:\image002.jpg")
    4.         img.Dispose()
    5.         s.Close()        
    6.         File.Copy("c:\image002.jpg", "c:\image001.jpg", True)
    7.         File.Delete("c:\image002.jpg")

  11. #11
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Originally posted by MrPolite

    dim fs as new filestream(imagePath)
    dim img as image = image.fromFile(fs)
    fs.close

    file.delete imagePath
    img.save (imagePath)

    No FromFile remember? Anyway

    VB Code:
    1. Dim myImage As Image
    2.             Dim fs As New IO.FileStream(filepath, IO.FileMode.Open)
    3.             myImage = Image.FromStream(fs)
    4.             fs.Close()
    5.  
    6.             myImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
    7.  
    8.             myImage.Save(filepath)
    9.             myImage.Dispose()
    10.             myImage = Nothing

  12. #12

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by nemaroller
    No FromFile remember? Anyway

    VB Code:
    1. Dim myImage As Image
    2.             Dim fs As New IO.FileStream(filepath, IO.FileMode.Open)
    3.             myImage = Image.FromStream(fs)
    4.             fs.Close()
    5.  
    6.             myImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
    7.  
    8.             myImage.Save(filepath)
    9.             myImage.Dispose()
    10.             myImage = Nothing
    eeh that was a typing mistake, that wont even compile (fromfile)

    hehe seems to be working now that's what I had before, dunno what I was doing wrong... (no, I didnt have any other mistypes like that , hehe)

    anyways THANKS ALOT, but another odd problem :
    it loads fine and all, but one thing is really weird. I cant apply any ImageAttributes to the image, unless I edit the image somehow first. For example if you replace your RotateFlip function with this function, you'll get an out of memory error. If you leave the RotateFlip function there, and then add this line AFTER it's done with RotateFlip, it would work fine:

    VB Code:
    1. '....
    2. fs.Close()
    3. MakeGrayscale (MyImage)  ' Gives out of memory error
    4. MyImage.save....
    this one works
    VB Code:
    1. '....
    2. fs.Close()
    3. myImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
    4. MakeGrayscale (MyImage)  
    5. MyImage.save....



    and this is where the imageAttribute is applied
    VB Code:
    1. Private Sub MakeGrayscale(ByVal img As Image)
    2.         Dim cMatrix As New ColorMatrix(New Single()() _
    3.                                  {New Single() {0.299, 0.299, 0.299, 0, 0}, _
    4.                                   New Single() {0.587, 0.587, 0.587, 0, 0}, _
    5.                                   New Single() {0.114, 0.114, 0.114, 0, 0}, _
    6.                                   New Single() {0, 0, 0, 1, 0}, _
    7.                                   New Single() {0, 0, 0, 0, 1}})
    8.  
    9.  
    10.         Dim imageAttr As New ImageAttributes
    11.         imageAttr.SetColorMatrix(cMatrix)
    12.  
    13.         Dim gr As Graphics = Graphics.FromImage(img)
    14.         gr.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttr)
    15.     End Sub


    the weird thing is that this was working fine before.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  13. #13

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    another thing I noticed, I'll included it for reference...
    if you try to open a readonly file with FileStream that way, you will get an error. You have to pass Read as the 3rd parameter when you create the filestream (dont have VS rightnow so I cant write the exact code)


    I still have that problem with applying the imageAttrbutes. Right now I'm getting around it by redrawing the picture on a Bitmap object after its loaded. something like,
    Dim bmp as new bitmap(img.width,img.height)
    dim gr as graphics = graphics.fromImage(bmp)
    gr.drawImage (img, 0,0,img.width, img.height)


    it's rather inefficient


    some other thing that I noticed, which has nothing to do with this post it's "on the side." I used to use the DrawImage method without specifiying the width and height (ie, gr.DrawImage(img, 0,0)) and I found out that for some images, it wont draw the image with a correct size if you dont specify the size)
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

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



Click Here to Expand Forum to Full Width