|
-
Oct 30th, 2003, 11:12 PM
#1
-
Oct 30th, 2003, 11:17 PM
#2
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:
Dim path As String = "c:\funny.jpg"
Dim fs As New IO.FileStream(path, IO.FileMode.Open)
Dim img As Image = Image.FromStream(fs)
Dim img2 As Image = DirectCast(img.Clone, Image)
fs.Flush()
fs.Close()
img.Dispose()
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!!
-
Oct 31st, 2003, 05:18 PM
#3
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!!
-
Nov 1st, 2003, 12:54 AM
#4
you could do something like this ...
VB Code:
[color=blue]Dim[/color] fs [color=blue]As New[/color] IO.FileStream("C:\tester.bmp", IO.FileMode.OpenOrCreate)
[color=blue]Dim[/color] img [color=blue]As[/color] Image = Image.FromStream(fs)
fs.Close()
IO.File.Delete("C:\tester.bmp") [color=green]'/// delete the exsisting image.[/color]
img.Save("C:\tester.bmp")
Or if you also want to modify the graphics...
VB Code:
[color=blue]Dim[/color] fs [color=blue]As New[/color] IO.FileStream("C:\tester.bmp", IO.FileMode.OpenOrCreate)
[color=blue]Dim[/color] img [color=blue]As[/color] Image = Image.FromStream(fs)
fs.Close()
IO.File.Delete("C:\tester.bmp") [color=green]'/// delete the exsisting image.[/color]
[color=blue]Dim[/color] grphics [color=blue]As[/color] Graphics = Graphics.FromImage(img)
grphics.DrawString("some stuff here", [color=blue]New[/color] Font("Tahoma", 10, FontStyle.Bold), [color=blue]New[/color] SolidBrush(Color.Red), 10, 10)
grphics.Save()
grphics.Dispose()
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]
-
Nov 1st, 2003, 01:09 AM
#5
or without even needing to delete the exsisting file ...
VB Code:
[color=blue]Dim[/color] img [color=blue]As[/color] Image = Image.FromFile("C:\tester.bmp")
[color=blue]Dim[/color] bmp [color=blue]As[/color] Bitmap = img
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]
-
Nov 1st, 2003, 02:45 AM
#6
Originally posted by dynamic_sysop
or without even needing to delete the exsisting file ...
VB Code:
[color=blue]Dim[/color] img [color=blue]As[/color] Image = Image.FromFile("C:\tester.bmp")
[color=blue]Dim[/color] bmp [color=blue]As[/color] Bitmap = img
bmp.Save("C:\tester.bmp")
neither one works
this is what I tried, I tried the one above in the quatations also:
VB Code:
Dim fs As New FileStream("C:\colormatrix.jpg", FileMode.Open)
Dim img As Image = Image.FromStream(fs)
fs.Close()
File.Delete("C:\colormatrix.jpg")
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!!
-
Nov 2nd, 2003, 07:10 PM
#7
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!!
-
Nov 2nd, 2003, 07:47 PM
#8
I wonder how many charact
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.
-
Nov 2nd, 2003, 08:36 PM
#9
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!!
-
Nov 2nd, 2003, 08:48 PM
#10
Member
Try this, save the image to temp file then replace the original with temp one, finally delete the temp file.
VB Code:
Dim s As FileStream = File.Open("c:\image001.jpg", FileMode.Open)
Dim img As Image = Image.FromStream(s)
img.Save("c:\image002.jpg")
img.Dispose()
s.Close()
File.Copy("c:\image002.jpg", "c:\image001.jpg", True)
File.Delete("c:\image002.jpg")
-
Nov 2nd, 2003, 09:09 PM
#11
I wonder how many charact
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:
Dim myImage As Image
Dim fs As New IO.FileStream(filepath, IO.FileMode.Open)
myImage = Image.FromStream(fs)
fs.Close()
myImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
myImage.Save(filepath)
myImage.Dispose()
myImage = Nothing
-
Nov 2nd, 2003, 09:59 PM
#12
Originally posted by nemaroller
No FromFile remember? Anyway
VB Code:
Dim myImage As Image
Dim fs As New IO.FileStream(filepath, IO.FileMode.Open)
myImage = Image.FromStream(fs)
fs.Close()
myImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
myImage.Save(filepath)
myImage.Dispose()
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:
'....
fs.Close()
MakeGrayscale (MyImage) ' Gives out of memory error
MyImage.save....
this one works
VB Code:
'....
fs.Close()
myImage.RotateFlip(RotateFlipType.Rotate90FlipNone)
MakeGrayscale (MyImage)
MyImage.save....
and this is where the imageAttribute is applied
VB Code:
Private Sub MakeGrayscale(ByVal img As Image)
Dim cMatrix As New ColorMatrix(New Single()() _
{New Single() {0.299, 0.299, 0.299, 0, 0}, _
New Single() {0.587, 0.587, 0.587, 0, 0}, _
New Single() {0.114, 0.114, 0.114, 0, 0}, _
New Single() {0, 0, 0, 1, 0}, _
New Single() {0, 0, 0, 0, 1}})
Dim imageAttr As New ImageAttributes
imageAttr.SetColorMatrix(cMatrix)
Dim gr As Graphics = Graphics.FromImage(img)
gr.DrawImage(img, New Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttr)
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!!
-
Nov 3rd, 2003, 11:47 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|