Tiff Image Resizer -This is Current one


{
public void Resizer(String Path, String workset, imRes imr)try{

{
imr.prgbar.Visible =
}
imr.prgbar.Value = 10;
imr.Update();
imEncode =
myEncoderParameters =
imr.prgbar.Value = 15;
imr.Update();











imr.prgbar.Value = 25;
imr.Update();

{

myEncoderParameter =
myEncoderParameter =
myEncoderParameter =

ms.GetBuffer();
myEncoderParameters.Param[0] = myEncoderParameter;
myEncoderParameters.Param[1] = myEncoderParameter;
myEncoderParameters.Param[2] = myEncoderParameter;
imr.prgbar.Value = 35;
imr.Update();
myImageCodecInfo = FindEncoder(

{
simage = CreateReducedImage(image,
}
if (imr.prgbar.Visible == false)true;Encoder.Compression;new EncoderParameters(3);string[] im = Directory.GetFiles(Path, "*.0*", SearchOption.AllDirectories);string[] im1 = Directory.GetFiles(Path, "*.1*", SearchOption.AllDirectories);string[] im2 = Directory.GetFiles(Path, "*.2*", SearchOption.AllDirectories);string[] im3 = Directory.GetFiles(Path, "*.3*", SearchOption.AllDirectories);string[] im4 = Directory.GetFiles(Path, "*.4*", SearchOption.AllDirectories);string[] im5 = Directory.GetFiles(Path, "*.5*", SearchOption.AllDirectories);string[] im6 = Directory.GetFiles(Path, "*.6*", SearchOption.AllDirectories);string[] im7 = Directory.GetFiles(Path, "*.7*", SearchOption.AllDirectories);string[] im8 = Directory.GetFiles(Path, "*.8*", SearchOption.AllDirectories);string[] im9 = Directory.GetFiles(Path, "*.9*", SearchOption.AllDirectories);string[] im10 = im.Union(im1).Union(im2).Union(im3).Union(im4).Union(im5).Union(im6).Union(im7).Union(im8).Union(im9).ToArray();for (int i = 0; i < im10.Length; i++)Image image = new Bitmap(im10[i]);new EncoderParameter(imEncode, Convert.ToInt64(EncoderValue.CompressionCCITT4));new EncoderParameter(Encoder.ColorDepth, 1);new EncoderParameter(Encoder.SaveFlag, Convert.ToInt64(EncoderValue.MultiFrame));MemoryStream ms = new MemoryStream();ImageFormat.Tiff);if (image.Height > image.Width)new Size(1800, 2200));else{
simage = CreateReducedImage(image,
}
imr.prgbar.Value = 50;
imr.Update();

imr.prgbar.Value = 70;
imr.Update();
new Size(2200, 1800));File.Move(im10[i].ToString(), im10[i].ToString() + "_BAK");// Save the bitmap as a TIFF file with group IV compression.simage.Save(im10[i].ToString(), myImageCodecInfo, myEncoderParameters);
image.Dispose();
simage.Dispose();
}
imr.prgbar.Value = 100;
imr.Update();
imr.lblstatus.Text =
}

{

}
"Completed";catch (Exception ex)MessageBox.Show(ex.Message, "Image Resizer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);finally{

}
}

{



{
img.Save(retStream,
retStream.Flush();
}
retVal =

}

{



{

{
tiffCodec = codec;

}
}
GC.SuppressFinalize(imEncode); public static Image ConvertToTiff(Stream imgStream)Image retVal = null;Stream retStream = new MemoryStream();using (Image img = Image.FromStream(imgStream, true, true))ImageFormat.Tiff);Image.FromStream(retStream, true, true);return retVal;public void convertor(String Path)ImageCodecInfo tiffCodec = null;ImageCodecInfo[] Encoders = ImageCodecInfo.GetImageEncoders();foreach (ImageCodecInfo codec in Encoders)if (codec.MimeType == "image/tiff")break;// Can't find the TIFF encoder
if (tiffCodec == null) return;// Set encoder to use no compression
ep.Param[0] =
(
EncoderParameters ep = new EncoderParameters();new EncoderParameter(Encoder.Compression,long)EncoderValue.CompressionNone);// Load a new image


Bitmap TempImg = new Bitmap(Path);string[] im = Directory.GetFiles(Path, "*.0*", SearchOption.AllDirectories);try{ // Attempt to save the imageTempImg.Save(im.ToString(), tiffCodec, ep);
}

{

}
TempImg.Dispose();
catch (Exception ex)MessageBox.Show("Error saving image: " + ex.Message);// Clean upep.Dispose();

}


{
private ImageCodecInfo GetEncoderInfo(String mimeType)try{


encoders =

{


}
}

{

}

}


{

int j;ImageCodecInfo[] encoders;ImageCodecInfo.GetImageEncoders();for (j = 0; j < encoders.Length; ++j)if (encoders[j].MimeType == mimeType)return encoders[j];catch (Exception ex)MessageBox.Show(ex.Message, "Image Resizer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);return null;private Image Createimage(Image imgOrig)// Bitmap newBM = new Bitmap(NewSize.Width, NewSize.Height, PixelFormat.Format32bppPArgb);
newBM.SetResolution(imgOrig.HorizontalResolution, imgOrig.VerticalResolution);

{
newGrapics.CompositingQuality =
newGrapics.SmoothingMode =
newGrapics.InterpolationMode =
newGrapics.DrawImage(imgOrig,
imgOrig.Dispose();
newGrapics.Dispose();
}

}

{



{

{

}
}
}
Bitmap newBM = new Bitmap(0,0, PixelFormat.Format32bppArgb);using (var newGrapics = Graphics.FromImage(newBM))CompositingQuality.HighSpeed;SmoothingMode.HighSpeed;InterpolationMode.HighQualityBicubic;new Rectangle(0, 0,0,0), 0, 0, 0,0, GraphicsUnit.Pixel);return newBM; public static ImageCodecInfo FindEncoder(ImageFormat format)if (format == null) throw new ArgumentNullException("format"); foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())if (codec.FormatID.Equals(format.Guid))return codec; return null;//private ImageCodecInfo GetEncoderInfo(String mimeType)//{// try// {// int j;// ImageCodecInfo[] encoders;// encoders = ImageCodecInfo.GetImageEncoders();// for (j = 0; j < encoders.Length; ++j)// {// if (encoders[j].MimeType == mimeType)// return encoders[j];// }// }// catch (Exception ex)// {// MessageBox.Show(ex.Message, "Image Resizer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);// }// return null;
//}
{
private Image CreateReducedImage(Image imgOrig, Size NewSize)try{
// Bitmap newBM = new Bitmap(NewSize.Width, NewSize.Height, PixelFormat.Format32bppPArgb);
newBM.SetResolution(imgOrig.HorizontalResolution, imgOrig.VerticalResolution);

{
newGrapics.CompositingQuality =
newGrapics.SmoothingMode =
newGrapics.InterpolationMode =
newGrapics.DrawImage(imgOrig,
imgOrig.Dispose();
newGrapics.Dispose();
}
Bitmap newBM = new Bitmap(NewSize.Width, NewSize.Height, PixelFormat.Format32bppArgb);using (var newGrapics = Graphics.FromImage(newBM))CompositingQuality.HighSpeed;SmoothingMode.HighSpeed;InterpolationMode.HighQualityBicubic;new Rectangle(0, 0, NewSize.Width, NewSize.Height), 0, 0, imgOrig.Width, imgOrig.Height, GraphicsUnit.Pixel);// Lock source bitmap in memory
BitmapData sourceData = newBM.LockBits(new Rectangle(0, 0, newBM.Width, newBM.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);// Copy image data to binary array


int imageSize = sourceData.Stride * sourceData.Height;byte[] sourceBuffer = new byte[imageSize];Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize);// Unlock source bitmapnewBM.UnlockBits(sourceData);
// Create destination bitmap
Bitmap destination = new Bitmap(newBM.Width, newBM.Height, PixelFormat.Format1bppIndexed);//Add to destination DPI value from the original bitmap DPI Valuedestination.SetResolution(newBM.HorizontalResolution, newBM.VerticalResolution);
// Lock destination bitmap in memory
BitmapData destinationData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);// Create destination bufferimageSize = destinationData.Stride * destinationData.Height;









byte[] destinationBuffer = new byte[imageSize];int sourceIndex = 0;int destinationIndex = 0;int pixelTotal = 0;byte destinationValue = 0;int pixelValue = 128;int height = newBM.Height;int width = newBM.Width;int threshold = 500;// Iterate lines
{
sourceIndex = y * sourceData.Stride;
destinationIndex = y * destinationData.Stride;
destinationValue = 0;
pixelValue = 128;
for (int y = 0; y < height; y++)// Iterate pixels
{
for (int x = 0; x < width; x++)// Compute pixel brightness (i.e. total of Red, Green, and Blue values)pixelTotal = sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2] + sourceBuffer[sourceIndex + 3];

{
destinationValue += (
}

{
destinationBuffer[destinationIndex] = destinationValue;
destinationIndex++;
destinationValue = 0;
pixelValue = 128;
}
if (pixelTotal > threshold)byte)pixelValue;if (pixelValue == 1)else{
pixelValue >>= 1;
}
sourceIndex += 4;
}

{
destinationBuffer[destinationIndex] = destinationValue;
}
}
if (pixelValue != 128)// Copy binary image data to destination bitmap
Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize);// Unlock destination bitmapdestination.UnlockBits(destinationData);
// blacknwhite(newBM);// return newBM;


}

{

}
return destination;catch (Exception ex)throw ex;finally{
//GC.SuppressFinalize(); }
}
/// <summary>/// ///////////////////// </summary>/// <param name="sourcePath"></param>/// <param name="destPath"></param>
{









{
srcBmp.SelectActiveFrame(




System.Runtime.InteropServices.
tempBmp.UnlockBits(tempBmd);


imageSize = destBmd.Stride * destBmd.Height;

destPage = GetBWPage(tempBmp, tempBmd, tempBuffer, destPage, destBmd, destBuffer, imageSize);

{
destFile = destPage;
destFile.Save(destPath, ici, ep);
}
private void ResaveTiffBlackWhite(string sourcePath, string destPath)FileStream fs = File.Open(sourcePath, FileMode.Open, FileAccess.Read);Bitmap srcBmp = (Bitmap)Bitmap.FromStream(fs);int totalPages = Convert.ToInt32(srcBmp.GetFrameCount(FrameDimension.Page) - 1);ImageCodecInfo ici = GetTiffCodec();EncoderParameters ep = GetBWEncoderParams();double resizeRatio = 1.0;Bitmap destFile = null;for (int currentPage = 0; currentPage <= totalPages; currentPage++)FrameDimension.Page, currentPage);Bitmap tempBmp = GetRGBPage(srcBmp, resizeRatio);BitmapData tempBmd = tempBmp.LockBits(new Rectangle(0, 0, tempBmp.Width, tempBmp.Height), ImageLockMode.ReadOnly, tempBmp.PixelFormat);int imageSize = tempBmd.Stride * tempBmd.Height;byte[] tempBuffer = new byte[imageSize + 1];Marshal.Copy(tempBmd.Scan0, tempBuffer, 0, imageSize);Bitmap destPage = new Bitmap(tempBmp.Width, tempBmp.Height, PixelFormat.Format1bppIndexed);BitmapData destBmd = destPage.LockBits(new Rectangle(0, 0, destPage.Width, destPage.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);byte[] destBuffer = new byte[imageSize + 1];if (currentPage == 0)else{
ep.Param[0] =
destFile.SaveAdd(destPage, ep);
}

{
ep.Param[0] =
destFile.SaveAdd(ep);
}
}
}

{

{

{

}
}

}

{

ep.Param[0] =
ep.Param[1] =
ep.Param[2] =

}

{


g.DrawImage(srcBmp,
g.Dispose();

}

{







{
srcIndex = y * srcBitmapData.Stride;
destIndex = y * destBitmapData.Stride;
destValue = 0;
pixValue = 128;

{
pixTotal = srcBuffer[srcIndex + 2];
pixTotal += srcBuffer[srcIndex + 2];
pixTotal += srcBuffer[srcIndex + 3];

{
destValue +=
}

{
destBuffer[destIndex] = destValue;
destIndex += 1;
destValue = 0;
pixValue = 128;
}
new EncoderParameter(Encoder.SaveFlag, Convert.ToInt64(EncoderValue.FrameDimensionPage));if (currentPage == Convert.ToInt32(srcBmp.GetFrameCount(FrameDimension.Page) - 1))new EncoderParameter(Encoder.SaveFlag, Convert.ToInt64(EncoderValue.Flush));private ImageCodecInfo GetTiffCodec()foreach (ImageCodecInfo ici in ImageCodecInfo.GetImageEncoders())if (ici.MimeType == "image/tiff")return ici;return null;private EncoderParameters GetBWEncoderParams()EncoderParameters ep = new EncoderParameters(3);new EncoderParameter(Encoder.SaveFlag, Convert.ToInt64(EncoderValue.MultiFrame));new EncoderParameter(Encoder.Compression, Convert.ToInt64(EncoderValue.CompressionCCITT4));new EncoderParameter(Encoder.ColorDepth, 1);return ep;private Bitmap GetRGBPage(Bitmap srcBmp, double resizeRatio)Bitmap tempBmp = new Bitmap(Convert.ToInt32(resizeRatio * srcBmp.Width), Convert.ToInt32(resizeRatio * srcBmp.Height), PixelFormat.Format32bppArgb);Graphics g = Graphics.FromImage(tempBmp);new Rectangle(0, 0, tempBmp.Width, tempBmp.Height), 0, 0, srcBmp.Width, srcBmp.Height, GraphicsUnit.Pixel);return tempBmp;private Bitmap GetBWPage(Bitmap srcBitmap, BitmapData srcBitmapData, byte[] srcBuffer, Bitmap destBitmap, BitmapData destBitmapData, byte[] destBuffer, int imageSize)int srcIndex = 0;int destIndex = 0;int pixTotal = 0;byte destValue = 0;byte pixValue = 0;int thresh = 600;for (int y = 0; y <= srcBitmap.Height - 1; y++)for (int x = 0; x <= srcBitmap.Width - 1; x++)if (pixTotal > thresh)Convert.ToByte(pixValue);if (pixValue == 1)else{
pixValue >>= 1;
}
srcIndex += 4;
}

{
destBuffer[destIndex] = destValue;
}
}
System.Runtime.InteropServices.
destBitmap.UnlockBits(destBitmapData);

}
if (pixValue != 128)Marshal.Copy(destBuffer, 0, destBitmapData.Scan0, imageSize);return destBitmap;/// <summary>/// /////////// </summary>/// <param name="imgBitmap"></param>
{
private void blacknwhite(Image imgBitmap)try{



{

{
c = img.GetPixel(i, j);

r =

g =

b =


{
r = 255;
g = 255;
b = 255;
}
Bitmap img = new Bitmap(imgBitmap);Color c;for (int i = 0; i < img.Width; i++)for (int j = 0; j < img.Height; j++)int r = 0;Convert.ToInt16(c.R);int g = 0;Convert.ToInt16(c.G);int b = 0;Convert.ToInt16(c.B);int ans = (r + g + b) / 3;if (ans > 128)else{
r = 0;
g = 0;
b = 0;
}
c =
img.SetPixel(i, j, c);
}
}
imgBitmap = img;
}

{

}
}



{
private void btnprocess_Click(object sender, EventArgs e)try{

prgbar.Visible =
prgbar.Value = 5;
prgbar.Update();
cs.Resizer(tData.Text.Trim(), tWorkset.Text.Trim(),


Resize cs = new Resize();true;this);// cs.convertor(tData.Text); }

{

}
}

{
catch (Exception ex)MessageBox.Show(ex.Message, "Image Resizer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);private void browse_Click(object sender, EventArgs e)try{


FolderBrowserDialog f = new FolderBrowserDialog();if (f.ShowDialog() == DialogResult.OK) // selecting directory{
tData.Text = f.SelectedPath;
}
}

{

}
}

{

}
catch (Exception ex)MessageBox.Show(ex.Message, "Image Resizer Information", MessageBoxButtons.OK, MessageBoxIcon.Information);private void btncancel_Click(object sender, EventArgs e)this.Close();
Color.FromArgb(r, g, b);catch (Exception ex)MessageBox.Show(ex.Message);

Comments