473,545 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compress Image

Thanks to Bob Powel (http://www.bobpowell.net/onebit.htm) I'm able to
compress an image. However, this method takes a long time to compress an
image. Does anyone know a quicker way? (The images I have to compress are
already in black and white).

Code used:
Bitmap bmpOrig =
(System.Drawing .Bitmap)Image.F romFile(OrigFil eName).Clone();

Bitmap objConvImage = new Bitmap(bmpOrig. Width, bmpOrig.Height,
PixelFormat.For mat1bppIndexed) ;
objConvImage.Se tResolution(300 ,300);

BitmapData bmd = objConvImage.Lo ckBits(new Rectangle(0, 0,
objConvImage.Wi dth, objConvImage.He ight),
ImageLockMode.R eadWrite, PixelFormat.For mat1bppIndexed );

for(int y=0; y < bmpOrig.Height; y++)
{
for(int x=0; x < bmpOrig.Width; x++)
{
if(bmpOrig.GetP ixel(x,y).GetBr ightness()>0.5f )
SetIndexedPixel (x,y,bmd,true);
}
}
objConvImage.Un lockBits(bmd);
objConvImage.Sa ve(FileName, _ImageCodecInfo , _ImageCodecPara ms);
objConvImage.Di spose();
bmpOrig.Dispose ();

and the functions:
private static unsafe void SetIndexedPixel (int x, int y, BitmapData bmd,
bool pixel)
{
byte* p = (byte*)bmd.Scan 0.ToPointer();
int index = y * bmd.Stride+(x>> 3);
byte mask = (byte)(0x80>>(x &0x7));
if(pixel)
p[index]|=mask;

else
p[index]&=(byte)(mask^0 xff);
}

private static ImageCodecInfo GetCodecInfo(st ring FileFormat)
{
ImageCodecInfo[] encoders = ImageCodecInfo. GetImageEncoder s();
for (int j=0; j < encoders.Length ; ++j)
{
if (encoders[j].MimeType == FileFormat)
return encoders[j];
}

return null;
}
Jul 22 '05 #1
3 2419

You can try this:

Bitmap bmp = new Bitmap(/*[input filename]*/);
bmp.Save(/*[output filename]*/, System.Drawing. Imaging.ImageFo rmat.Jpeg);

By changing the System.Drawing. Imaging.ImageFo rmat value, you can change
the type to convert to. Basically, all types other than BMP are lossy
(well, other than vectors, but never mind about those)
Jul 22 '05 #2
Blackice wrote:

You can try this:

Bitmap bmp = new Bitmap(/*[input filename]*/);
bmp.Save(/*[output filename]*/, System.Drawing. Imaging.ImageFo rmat.Jpeg);

By changing the System.Drawing. Imaging.ImageFo rmat value, you can change
the type to convert to. Basically, all types other than BMP are lossy
(well, other than vectors, but never mind about those)


TIF isn't lossy neither is RGA and if the bitmap is already in 8bit
color, GIF isn't lossy either. I have no idea if .net supports any of
these formats though :)

--
Rinze van Huizen
C-Services Holland b.v.
Jul 22 '05 #3
I know that I can change the type this way. However, the saved imaged isn't
compressed. I want to compress the tiff image. It only holds black and white.

Edward

"Blackice" wrote:

You can try this:

Bitmap bmp = new Bitmap(/*[input filename]*/);
bmp.Save(/*[output filename]*/, System.Drawing. Imaging.ImageFo rmat.Jpeg);

By changing the System.Drawing. Imaging.ImageFo rmat value, you can change
the type to convert to. Basically, all types other than BMP are lossy
(well, other than vectors, but never mind about those)

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
11652
by: Jigar Mehta | last post by:
Hye Friends!!, Happy New Year!! I am Jigar Mehta from India. Currently I am working on a project in which I need to compress some images on the harddisk... I want to make an engine that reads whole BMP image and then compress it.. The target location can be of any type.. BMP, TIFF, JPG or any.. Some data loss will be negligible because all...
3
402
by: Edward | last post by:
Thanks to Bob Powel (http://www.bobpowell.net/onebit.htm) I'm able to compress an image. However, this method takes a long time to compress an image. Does anyone know a quicker way? (The images I have to compress are already in black and white). Code used: Bitmap bmpOrig = (System.Drawing.Bitmap)Image.FromFile(OrigFileName).Clone(); ...
1
11488
by: ironpythonster | last post by:
Hi everyone, this should be a quick question. I am writing a function to compress the Jpeg file,with the API Image.Save(...) . the code as follow that: public static void Save(string imagePath, Bitmap image, int quality) { if (JpegQuality.Codec == null) image.Save(imagePath,
3
1492
by: Samuel | last post by:
Hi Is there a method to comress Jpg images so it will download quicker to the user's browsers? Thank you, Samuel
2
9358
by: Zuhaib Hyder | last post by:
string stFileName = "abc"; string stFullFilePath = Server.MapPath("images/" + stFileName + ".jpg"); System.Drawing.Image i = System.Drawing.Image.FromFile(stFullFilePath); EncoderParameters eps = new EncoderParameters(1); eps.Param = new EncoderParameter( Encoder.Quality, 40 ); ...
5
4910
by: zgh1970 | last post by:
Hi, Friends, default DB2 compression library. I am wondering if this option will have any new restriction on RESTORE in the following. (Can I used that backup imsage for restore at the follwoing compatible situations?) : There are operating system considerations when using the RESTORE DATABASE command:
2
4022
by: nicky77 | last post by:
Hi there, I'm brand new to ASP (from PHP background) and i am developing some content management for a web site. I'm having a problem with the fact that I want to allow image files to be uploaded to a remote web server, as the hosting package the web site is on is IIS6 and has a default file upload size limit of 200kb. As it's a shared hosting...
4
2144
by: Lennart | last post by:
I heard a rumor that compress is only availible in Enterprise Edition in 9.5, can anyone confirm/reject this rumor? /Lennart
6
2228
by: mohammedsiddig | last post by:
Hello php programmer ! how i can compress image and storage it on oracle ? the compress must reduce the original size of the image to more than half of original size
0
7499
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7689
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7943
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7786
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.