C# .NET reading a bitmap, resizing / rescaling and saving as a TIFF
Here's some code to read in a bitmap and save a new bitmap at a quarter of the size. Note. a boilerplate method is needed to get the encoder for the write format. This makes it easy to change from TIFF to JPEG, PNG, etc.
// Load the bitmap from first argument filename
Bitmap bmIn = new Bitmap(inputFilename);
// Create a new bitmap from the first, scaling down as we go
Bitmap bmOut = new Bitmap(bmIn, new Size(bmIn.Width/2, bmIn.Height/2));
// Create an parameter set that we use to save the file
EncoderParameters encParams = new EncoderParameters(3);
// Set some parameters
encParams.Param[0] = new EncoderParameter(Encoder.ColorDepth, 24L);
encParams.Param[1] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionNone);
encParams.Param[2] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
// Save bitmap to second argument filename
bmOut2.Save(outputFilename, GetEncoder(ImageFormat.Tiff), encParams);
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}

0 Comments:
Post a Comment
<< Home