473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.FromFile(OrigFileName ).Clone();

Bitmap objConvImage = new Bitmap(bmpOrig.Width, bmpOrig.Height,
PixelFormat.Format1bppIndexed);
objConvImage.SetResolution(300,300);

BitmapData bmd = objConvImage.LockBits(new Rectangle(0, 0,
objConvImage.Width, objConvImage.Height),
ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed );

for(int y=0; y < bmpOrig.Height; y++)
{
for(int x=0; x < bmpOrig.Width; x++)
{
if(bmpOrig.GetPixel(x,y).GetBrightness()>0.5f)
SetIndexedPixel(x,y,bmd,true);
}
}
objConvImage.UnlockBits(bmd);
objConvImage.Save(FileName, _ImageCodecInfo, _ImageCodecParams);
objConvImage.Dispose();
bmpOrig.Dispose();

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

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

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

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

You can try this:

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

By changing the System.Drawing.Imaging.ImageFormat 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.ImageFormat.Jpeg);

By changing the System.Drawing.Imaging.ImageFormat 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.ImageFormat.Jpeg);

By changing the System.Drawing.Imaging.ImageFormat 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
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...
3
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...
1
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...
3
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
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); ...
5
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...
2
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...
4
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
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.