Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region resize
- private void btnResize_Click(object sender, EventArgs e)
- {
- if (!tbHeight.Equals("") && !tbWidth.Equals(""))
- {
- //img = resizeImage(pbShow.Image, new Size(Convert.ToInt32(tsWidth.Text), Convert.ToInt32(tsHeight.Text)));
- //pbShow.SizeMode = PictureBoxSizeMode.CenterImage;
- //img = resizeImage(img, new Size(Convert.ToInt32(tbWidth.Text), Convert.ToInt32(tbHeight.Text)));
- //img = pbShow.Image;
- img = resize.ResizeImage(new Size(Convert.ToInt32(tbWidth.Text), Convert.ToInt32(tbHeight.Text)), img, false);
- SetImage2();
- }
- }
- private void ts50_Click(object sender, EventArgs e)
- {
- img = pbShow.Image;
- int newWidth = (int)(img.Width * 0.5);
- int newHeight = (int)(img.Height * 0.5);
- img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
- SetImage2();
- }
- private void ts100_Click(object sender, EventArgs e)
- {
- img = pbShow.Image;
- int newWidth = (int)(img.Width * 1);
- int newHeight = (int)(img.Height * 1);
- img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
- SetImage2();
- }
- private void ts150_Click(object sender, EventArgs e)
- {
- img = pbShow.Image;
- int newWidth = (int)(img.Width * 1.5);
- int newHeight = (int)(img.Height * 1.5);
- img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
- SetImage2();
- }
- private void ts200_Click(object sender, EventArgs e)
- {
- img = pbShow.Image;
- int newWidth = (int)(img.Width * 2);
- int newHeight = (int)(img.Height * 2);
- img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
- SetImage2();
- }
- private void ts300_Click(object sender, EventArgs e)
- {
- img = pbShow.Image;
- int newWidth = (int)(img.Width * 3);
- int newHeight = (int)(img.Height * 3);
- img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
- SetImage2();
- }
- private void ts500_Click(object sender, EventArgs e)
- {
- img = pbShow.Image;
- int newWidth = (int)(img.Width * 5);
- int newHeight = (int)(img.Height * 5);
- splitContainer1.Panel1.AutoScroll = true;
- img = resize.ResizeImage(new Size(newWidth, newHeight), img, true);
- SetImage2();
- }
- #endregion
- private Bitmap bmap;
- public Image ResizeImage(Size size, Image image, bool preserveAspectRatio)
- {
- int newWidth, newHeight;
- if (preserveAspectRatio)
- {
- int origWidth = image.Width;
- int origHeight = image.Height;
- float percentWidth = (float)size.Width / (float)origWidth;
- float percentHeight = (float)size.Height / (float)origHeight;
- float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
- newWidth = (int)(origWidth * percent);
- newHeight = (int)(origHeight * percent);
- }
- else
- {
- newHeight = size.Height;
- newWidth = size.Width;
- }
- Image newImg = new Bitmap(newWidth, newHeight);
- using (Graphics gfx = Graphics.FromImage(newImg))
- {
- gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
- gfx.DrawImage(image, 0, 0, newWidth, newHeight);
- }
- return newImg;
- }
- public Image reSize(int newWidth, int newHeight, Image img)
- {
- if (newWidth != 0 && newHeight != 0)
- {
- Bitmap temp = (Bitmap)img;
- bmap = new Bitmap(newWidth, newHeight, temp.PixelFormat);
- double nWidthFactor = (double)temp.Width / (double)newWidth;
- double nHeightFactor = (double)temp.Height / (double)newHeight;
- double fx, fy, nx, ny;
- int cx, cy, fr_x, fr_y;
- Color color1 = new Color();
- Color color2 = new Color();
- Color color3 = new Color();
- Color color4 = new Color();
- byte nRed, nGreen, nBlue;
- byte bp1, bp2;
- for (int x = 0; x < bmap.Width; ++x)
- {
- for (int y = 0; y < bmap.Height; ++y)
- {
- fr_x = (int)Math.Floor(x * nWidthFactor);
- fr_y = (int)Math.Floor(y * nHeightFactor);
- cx = fr_x + 1;
- if (cx >= temp.Width) cx = fr_x;
- cy = fr_y + 1;
- if (cy >= temp.Height) cy = fr_y;
- fx = x * nWidthFactor - fr_x;
- fy = y * nHeightFactor - fr_y;
- nx = 1.0 - fx;
- ny = 1.0 - fy;
- color1 = temp.GetPixel(fr_x, fr_y);
- color2 = temp.GetPixel(cx, fr_y);
- color3 = temp.GetPixel(fr_x, cy);
- color4 = temp.GetPixel(cx, cy);
- // Blue
- bp1 = (byte)(nx * color1.B + fx * color2.B);
- bp2 = (byte)(nx * color3.B + fx * color4.B);
- nBlue = (byte)(ny * (double)(bp1) + fy * (double)(bp2));
- // Green
- bp1 = (byte)(nx * color1.G + fx * color2.G);
- bp2 = (byte)(nx * color3.G + fx * color4.G);
- nGreen = (byte)(ny * (double)(bp1) + fy * (double)(bp2));
- // Red
- bp1 = (byte)(nx * color1.R + fx * color2.R);
- bp2 = (byte)(nx * color3.R + fx * color4.R);
- nRed = (byte)(ny * (double)(bp1) + fy * (double)(bp2));
- bmap.SetPixel(x, y, System.Drawing.Color.FromArgb
- (255, nRed, nGreen, nBlue));
- }
- }
- Graphics g = Graphics.FromImage((Image)bmap);
- g.CompositingMode = CompositingMode.SourceOver;
- g.CompositingQuality = CompositingQuality.HighQuality;
- if (newWidth >= img.Width && newHeight >= img.Height)
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- else
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.PixelOffsetMode = PixelOffsetMode.HighQuality;
- g.SmoothingMode = SmoothingMode.HighQuality;
- g.DrawImage(img, 0, 0, newWidth, newHeight);
- g.Dispose();
- }
- return (Bitmap)bmap;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement