How to compress System.Drawing.Bitmap without saving the file

JumpyNET

Centurion
Joined
Apr 4, 2005
Messages
196
What I do here is I
1) load an image file
2) blurr the image
3) pass the image to a third party component (which will save the image inside a pdf file) that accepts System.Drawing.Image, but does not do any post processing like compressing it, and the resulting big file size suggest that the file is saved as a bmp file. (If pass an unblurred image the third party component it saves the image in the original image format and results in the same small file size as the original image.)

So my question is how do I compress the blurred image without making a temporary copy of it to a hard disk?

[Vb]
Dim Filter As New AForge.Imaging.Filters.GaussianBlur(0.1, 4)
Dim SourceImg As System.Drawing.Bitmap = AForge.Imaging.Image.FromFile("E:\Small.png")
Dim BlurredImage As System.Drawing.Bitmap = Filter.Apply(SourceImg)
Dim Compressed As New System.IO.FileStream("?")
BlurredImage.Save(Compressed, System.Drawing.Imaging.ImageFormat.Png)
Me.BackgroundImage = Bitmap.FromStream(Compressed)
[/CODE]
 
When you are saving the Image using BlurredImage.Save the first parameter can be any Stream derived class not just a FileStream.

You could do something like
Visual Basic:
Dim Filter As New AForge.Imaging.Filters.GaussianBlur(0.1, 4)
Dim SourceImg As System.Drawing.Bitmap = AForge.Imaging.Image.FromFile("E:\Small.png")
Dim BlurredImage As System.Drawing.Bitmap = Filter.Apply(SourceImg)
Dim data() as Byte
Dim Compressed As New System.IO.MemoryStream(data)
BlurredImage.Save(Compressed, System.Drawing.Imaging.ImageFormat.Png)
Me.BackgroundImage = Bitmap.FromStream(Compressed)

which should work.
 
Back
Top