• After more than 30 years running websites and forums I am retiring.

    I have made many friends through the years. I will cherish my time getting to know you. I wish you all the best. This was not an easy decision to make. The cost to keep the communities running has gotten to the point where it's just too expensive. Security certificates, hosting cost, software renewals and everything else has increased threefold. While costs are up ad revenue is down. It's no longer viable to keep things running.

    All sites will be turned off on Thursday 30 November 2023. If you are interested in acquiring any of the websites I own you can Email Schwarz Network.

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

JumpyNET

Centurion
Joined
Apr 4, 2005
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]
 

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
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.
 
Top Bottom