Friday, August 2, 2013

Image Optimize code in c#

    protected void BtnOptimse_Click(object sender, EventArgs e) 
 { 
 string strFilename = "AMD-143.tif"; 
 string strDefaultPath = ""; 
 string strDocPath = ""; 
 //  BtnPrint.Visible = false; 
 //  Control ctrl = (Control)Panel1; 
 //  RefreshImage1(); 
 // ImageUtil obj = new ImageUtil(); 
 string fileName = strFilename; 
 string sourcePath = @"C:\Users\sanjay.bhansali\Documents\BigAnt\My Received Files";
 string targetPath = @"C:\Users\sanjay.bhansali\Documents\BigAnt\My Received Files\temp1"; 
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
 string destFile = System.IO.Path.Combine(targetPath, fileName);
 if (!System.IO.Directory.Exists(targetPath)) 
 { 
 System.IO.Directory.CreateDirectory(targetPath); 
 } 
 // System.IO.File.Copy(sourceFile, destFile, true); 
 string c = targetPath + "\\" + fileName; 
 // // I need to do something here to save the Image in TIFF format with the same exact characteristics. 
// System.Drawing.Bitmap.FromFile(destFile).Save(destFile + ".png", System.Drawing.Imaging.ImageFormat.Png); 
 // System.Drawing.Bitmap.FromFile(destFile + ".png").Save(destFile, System.Drawing.Imaging.ImageFormat.Png); 
 //================================ 
 try         { 
 ////FileStream fs = new FileStream(c, FileMode.Open, FileAccess.Read); 
 ////Bitmap bm = (Bitmap)Bitmap.FromStream(fs); 
 ////Bitmap image; 
 System.Drawing.Image image = System.Drawing.Image.FromFile(c); 
 byte[] imgData = getData(c); 
 System.Drawing.Image img = System.Drawing.Image.FromStream(new MemoryStream(imgData)); 
 Bitmap oBitmap; 
 oBitmap = new Bitmap(new Bitmap(img)); 
 //Dim oGraphic As Graphics 
 Graphics oGraphic = null; 
 //' Here create a new bitmap object of the same height and width of the image. 
 Bitmap bmpNew = new Bitmap(oBitmap.Width, oBitmap.Height); 
 //oGraphic = Graphics.FromImage(bmpNew) 
 oGraphic = Graphics.FromImage(bmpNew); 
 //oGraphic.DrawImage(oBitmap, New Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, oBitmap.Width, oBitmap.Height, GraphicsUnit.Pixel) 
 oGraphic.DrawImage(oBitmap, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, oBitmap.Width, oBitmap.Height, GraphicsUnit.Pixel); 
//' Release the lock on the image file. Of course, 
 //' image from the image file is existing in Graphics object 
 SolidBrush oBrush = new SolidBrush(Color.Black); 
 //Dim ofont As New Font("Arial", 8 ) 
 Font ofont = new Font("Arial", 50); 
 //oGraphic.DrawString("Some text to write", ofont, oBrush, 10, 10) 
 oGraphic.DrawString("SomeText to Write", ofont, oBrush, 500, 500); 
 ofont.Dispose(); 
 oBrush.Dispose(); 
 oBitmap.Dispose(); 
 oGraphic.Dispose(); 
 // Lets start to change the image quality 
 // System.Drawing.Bitmap.FromFile(destFile).Save(destFile + ".png", System.Drawing.Imaging.ImageFormat.Png); 
 // System.Drawing.Bitmap.FromFile(destFile + ".png").Save(destFile, System.Drawing.Imaging.ImageFormat.Png); 
 //oBitmap.Save("D:\testexample.jpg"); 
 oBitmap = bmpNew; 
 bmpNew.Save(destFile, ImageFormat.Jpeg); 
 // get the tiff codec info 
 /* ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/tiff"); 
 // Create an Encoder object based on the GUID for the Compression parameter category 
 System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Compression; 
 // create encode parameters 
 EncoderParameters myEncoderParameters = new EncoderParameters(1); 
 EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionCCITT4); 
 myEncoderParameters.Param[0] = myEncoderParameter;
 oBitmap = bmpNew;     
 
 // save as a tiff
 oBitmap.Save(@"C:\Users\sanjay.bhansali\Documents\BigAnt\My Received Files\temp1\test.tif", myImageCodecInfo, myEncoderParameters); 
 */
 } 
 catch (Exception ex)
 { 
 } 
 ////========================================================== 
 //System.Drawing.Image image = System.Drawing.Image.FromFile(c); 
 //byte[] imgData = getData(c); 
 //System.Drawing.Image img = System.Drawing.Image.FromStream(new MemoryStream(imgData)); 
//Bitmap img1 = new Bitmap(new Bitmap(img)); 
 // Graphics graphics = null; 
// graphics = Graphics.FromImage(img1); 
 // Font ofont = new Font("Arial", 8); 
// SolidBrush oBrush = new SolidBrush(Color.Black); 
 //// RasterEdgeImaging.Font font = new RasterEdgeImaging.Font("Times New Roman", 16, FontStyle.Bold);
 // //RasterEdgeImaging.Brushes.SolidBrush brush=new RasterEdgeImaging.Brushes.SolidBrush(); 
 // //image.CreateWatermark("watermark", ofont, oBrush, new PointF(image.Width / 2, image.Height / 2)); 
// graphics.DrawString("SomeText to Write", ofont, oBrush, new PointF(image.Width / 2, image.Height / 2)); 
 // ofont.Dispose(); 
 // oBrush.Dispose(); 
 // graphics.Dispose(); 
 // image.Save(@"C:\1-watermark.bmp"); 
 //============================= 
 //PrintHelper.PrintWebControl(ctrl); 
 }          public byte[] getData(string filePath) 
 {           FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
 BinaryReader br = new BinaryReader(fs); 
 byte[] data = br.ReadBytes((int)fs.Length); 
 br.Close();           fs.Close(); 
 return data; 
 }
 

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment