Advertisement
20myloves13

imager

Nov 24th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.20 KB | None | 0 0
  1. #region resize
  2.         private void btnResize_Click(object sender, EventArgs e)
  3.         {
  4.             if (!tbHeight.Equals("") && !tbWidth.Equals(""))
  5.             {
  6.                 //img = resizeImage(pbShow.Image, new Size(Convert.ToInt32(tsWidth.Text), Convert.ToInt32(tsHeight.Text)));
  7.                 //pbShow.SizeMode = PictureBoxSizeMode.CenterImage;
  8.                 //img = resizeImage(img, new Size(Convert.ToInt32(tbWidth.Text), Convert.ToInt32(tbHeight.Text)));
  9.                 //img = pbShow.Image;
  10.                 img = resize.ResizeImage(new Size(Convert.ToInt32(tbWidth.Text), Convert.ToInt32(tbHeight.Text)), img, false);
  11.                 SetImage2();
  12.                
  13.             }
  14.         }
  15.  
  16.         private void ts50_Click(object sender, EventArgs e)
  17.         {
  18.             img = pbShow.Image;
  19.  
  20.             int newWidth = (int)(img.Width * 0.5);
  21.             int newHeight = (int)(img.Height * 0.5);
  22.  
  23.             img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
  24.             SetImage2();
  25.         }
  26.  
  27.         private void ts100_Click(object sender, EventArgs e)
  28.         {
  29.             img = pbShow.Image;
  30.  
  31.             int newWidth = (int)(img.Width * 1);
  32.             int newHeight = (int)(img.Height * 1);
  33.  
  34.             img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
  35.             SetImage2();
  36.         }
  37.  
  38.         private void ts150_Click(object sender, EventArgs e)
  39.         {
  40.             img = pbShow.Image;
  41.  
  42.             int newWidth = (int)(img.Width * 1.5);
  43.             int newHeight = (int)(img.Height * 1.5);
  44.  
  45.             img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
  46.             SetImage2();
  47.         }
  48.  
  49.         private void ts200_Click(object sender, EventArgs e)
  50.         {
  51.             img = pbShow.Image;
  52.  
  53.             int newWidth = (int)(img.Width * 2);
  54.             int newHeight = (int)(img.Height * 2);
  55.  
  56.             img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
  57.             SetImage2();
  58.         }
  59.  
  60.         private void ts300_Click(object sender, EventArgs e)
  61.         {
  62.             img = pbShow.Image;
  63.  
  64.             int newWidth = (int)(img.Width * 3);
  65.             int newHeight = (int)(img.Height * 3);
  66.  
  67.             img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
  68.             SetImage2();
  69.         }
  70.  
  71.         private void ts500_Click(object sender, EventArgs e)
  72.         {
  73.             img = pbShow.Image;
  74.  
  75.             int newWidth = (int)(img.Width * 5);
  76.             int newHeight = (int)(img.Height * 5);
  77.             splitContainer1.Panel1.AutoScroll = true;
  78.             img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
  79.             SetImage2();
  80.         }
  81.  
  82.         #endregion
  83.  
  84. private Bitmap bmap;
  85.  
  86.         public Image ResizeImage(Size size, Image image, bool preserveAspectRatio)
  87.         {
  88.             int newWidth, newHeight;
  89.  
  90.             if (preserveAspectRatio)
  91.             {
  92.                 int origWidth = image.Width;
  93.                 int origHeight = image.Height;
  94.                 float percentWidth = (float)size.Width / (float)origWidth;
  95.                 float percentHeight = (float)size.Height / (float)origHeight;
  96.                 float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
  97.  
  98.                 newWidth = (int)(origWidth * percent);
  99.                 newHeight = (int)(origHeight * percent);
  100.             }
  101.             else
  102.             {
  103.                 newHeight = size.Height;
  104.                 newWidth = size.Width;
  105.             }
  106.  
  107.             Image newImg = new Bitmap(newWidth, newHeight);
  108.             using (Graphics gfx = Graphics.FromImage(newImg))
  109.             {
  110.                 gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
  111.                 gfx.DrawImage(image, 0, 0, newWidth, newHeight);
  112.             }
  113.  
  114.             return newImg;
  115.         }
  116.  
  117.         public Image reSize(int newWidth, int newHeight, Image img)
  118.         {
  119.             if (newWidth != 0 && newHeight != 0)
  120.             {
  121.                 Bitmap temp = (Bitmap)img;
  122.                 bmap = new Bitmap(newWidth, newHeight, temp.PixelFormat);
  123.  
  124.                 double nWidthFactor = (double)temp.Width / (double)newWidth;
  125.                 double nHeightFactor = (double)temp.Height / (double)newHeight;
  126.  
  127.                 double fx, fy, nx, ny;
  128.                 int cx, cy, fr_x, fr_y;
  129.                 Color color1 = new Color();
  130.                 Color color2 = new Color();
  131.                 Color color3 = new Color();
  132.                 Color color4 = new Color();
  133.                 byte nRed, nGreen, nBlue;
  134.  
  135.                 byte bp1, bp2;
  136.  
  137.                 for (int x = 0; x < bmap.Width; ++x)
  138.                 {
  139.                     for (int y = 0; y < bmap.Height; ++y)
  140.                     {
  141.  
  142.                         fr_x = (int)Math.Floor(x * nWidthFactor);
  143.                         fr_y = (int)Math.Floor(y * nHeightFactor);
  144.                         cx = fr_x + 1;
  145.                         if (cx >= temp.Width) cx = fr_x;
  146.                         cy = fr_y + 1;
  147.                         if (cy >= temp.Height) cy = fr_y;
  148.                         fx = x * nWidthFactor - fr_x;
  149.                         fy = y * nHeightFactor - fr_y;
  150.                         nx = 1.0 - fx;
  151.                         ny = 1.0 - fy;
  152.  
  153.                         color1 = temp.GetPixel(fr_x, fr_y);
  154.                         color2 = temp.GetPixel(cx, fr_y);
  155.                         color3 = temp.GetPixel(fr_x, cy);
  156.                         color4 = temp.GetPixel(cx, cy);
  157.  
  158.                         // Blue
  159.                         bp1 = (byte)(nx * color1.B + fx * color2.B);
  160.  
  161.                         bp2 = (byte)(nx * color3.B + fx * color4.B);
  162.  
  163.                         nBlue = (byte)(ny * (double)(bp1) + fy * (double)(bp2));
  164.  
  165.                         // Green
  166.                         bp1 = (byte)(nx * color1.G + fx * color2.G);
  167.  
  168.                         bp2 = (byte)(nx * color3.G + fx * color4.G);
  169.  
  170.                         nGreen = (byte)(ny * (double)(bp1) + fy * (double)(bp2));
  171.  
  172.                         // Red
  173.                         bp1 = (byte)(nx * color1.R + fx * color2.R);
  174.  
  175.                         bp2 = (byte)(nx * color3.R + fx * color4.R);
  176.  
  177.                         nRed = (byte)(ny * (double)(bp1) + fy * (double)(bp2));
  178.  
  179.                         bmap.SetPixel(x, y, System.Drawing.Color.FromArgb
  180.                 (255, nRed, nGreen, nBlue));
  181.                     }
  182.                 }
  183.                 Graphics g = Graphics.FromImage((Image)bmap);
  184.  
  185.                 g.CompositingMode = CompositingMode.SourceOver;
  186.                 g.CompositingQuality = CompositingQuality.HighQuality;
  187.  
  188.                 if (newWidth >= img.Width && newHeight >= img.Height)
  189.                     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  190.                 else
  191.                     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  192.  
  193.                 g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  194.                 g.SmoothingMode = SmoothingMode.HighQuality;
  195.  
  196.                 g.DrawImage(img, 0, 0, newWidth, newHeight);
  197.                 g.Dispose();
  198.             }
  199.             return (Bitmap)bmap;
  200.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement