Image Compression and Watermark Text In Asp.Net C#

   public void CreatePicture(Stream Fs,string uploadFilename, bool BinaryWrite)
            {
               string WorkingDirectory = HttpContext.Current.Server.MapPath("Images/DoctorImg/");
          
            /*  Bitmap bmpUpload = new Bitmap(Fs, false);
                Graphics graphicsObj = Graphics.FromImage(bmpUpload);
                Brush brush = new SolidBrush(Color.Gray);
                Point postionWaterMark = new Point((bmpUpload.Width /10), (bmpUpload.Height/10));
                graphicsObj.DrawString("Copyright@2014", new System.Drawing.Font("verdana",20, FontStyle.Bold, GraphicsUnit.Pixel), brush, postionWaterMark);
                bmpUpload.Save(WorkingDirectory + "/" + System.IO.Path.GetFileName(uploadFilename),ImageFormat.Jpeg);   
               */ 
                   System.Drawing.Image image = System.Drawing.Image.FromStream(Fs);
                   int w1 = image.Width;
                   int h1 = image.Height;
             
                   System.Drawing.Bitmap b1 = new System.Drawing.Bitmap(w1, h1);
                   Bitmap b = new Bitmap(255, 255);
                   Graphics g1 = Graphics.FromImage(b);
                   g1.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                   g1.FillRectangle(Brushes.White, 0, 0, 255, 255);

                   g1.DrawImage(b, 0, 0, 255, 255);

                   MemoryStream ms = new MemoryStream();
                   b.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                   int w =b.Width;
                   int h =b.Height;

                //Bitmap b = new Bitmap(w, h, PixelFormat.Format24bppRgb);
                //b = b1;
                //b.SetResolution(80, 80);
                //b.GetThumbnailImage(w, w, null, IntPtr.Zero);

                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage((System.Drawing.Image)b);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.Clear(System.Drawing.Color.Transparent);
                g.DrawImage(image, 0, 0, w, h);
                System.Drawing.Font drawFont = new System.Drawing.Font("Verdana", 17);
                System.Drawing.SolidBrush drawBrush = new    System.Drawing.SolidBrush(System.Drawing.Color.White);

            //  int xPixlesFromBottom = (int)(image.Width/2);
            //  float yPosFromBottom = ((image .Height -30));

                int xPixlesFromBottom = (int)(w/2);
                float yPosFromBottom = ((h -30));

                g.DrawString("Copyright@2014", drawFont, drawBrush, xPixlesFromBottom, yPosFromBottom);
                System.Drawing.Image newImage = (System.Drawing.Image)b;
                newImage.Save(WorkingDirectory + "/" + System.IO.Path.GetFileName(uploadFilename), ImageFormat.Jpeg); 
          
   }

Comments

Popular posts from this blog

Validate Mobile Number with 10 Digits in ASP.Net