C#实现缩放和剪裁图片的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
目标其实都是
public
static
Bitmap GetThumbnail(Bitmap b,
int
destHeight,
int
destWidth)
{
System.Drawing.Image imgSource = b;
System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
int
sW = 0, sH = 0;
// 按比例缩放
int
sWidth = imgSource.Width;
int
sHeight = imgSource.Height;
if
(sHeight > destHeight || sWidth > destWidth)
{
if
((sWidth * destHeight) > (sHeight * destWidth))
{
sW = destWidth;
sH = (destWidth * sHeight) / sWidth;
}
else
{
sH = destHeight;
sW = (sWidth * destHeight) / sHeight;
}
}
else
{
sW = sWidth;
sH = sHeight;
}
Bitmap outBmp =
new
Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(outBmp);
g.Clear(Color.Transparent);
// 设置画布的描绘质量
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgSource,
new
Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
g.Dispose();
// 以下代码为保存图片时,设置压缩质量
EncoderParameters encoderParams =
new
EncoderParameters();
long
[] quality =
new
long
[1];
quality[0] = 100;
EncoderParameter encoderParam =
new
EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
imgSource.Dispose();
return
outBmp;
}
实例:
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CompressImageTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Image SourceImage = null; private void button_SelectImage_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); string filePath = ""; if(openFileDialog.ShowDialog()==DialogResult.OK) { filePath = openFileDialog.FileName; } SourceImage=Image.FromFile(filePath); this.textBox_CurrentWidth.Text = Convert.ToString(SourceImage.Width); this.textBox_CurrentHeight.Text = Convert.ToString(SourceImage.Height); this.textBox_SetWidth.Text = ""; this.textBox_SetHeight.Text = ""; this.pictureBox1.Image = SourceImage; SaveImageToStream(SourceImage);//保存图像 } public Bitmap GetThumbnail(Bitmap b, int destWidth, int destHeight) { System.Drawing.Image imgSource = b; System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat; int sW = 0, sH = 0; // 按比例缩放 int sWidth = imgSource.Width; int sHeight = imgSource.Height; if (sHeight > destHeight || sWidth > destWidth) { if ((sWidth * destHeight) > (sHeight * destWidth)) { sW = destWidth; sH = (destWidth * sHeight) / sWidth; } else { sH = destHeight; sW = (sWidth * destHeight) / sHeight; } } else { sW = sWidth; sH = sHeight; } Bitmap outBmp = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage(outBmp); g.Clear(Color.Transparent); // 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel); g.Dispose(); // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 100; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); encoderParams.Param[0] = encoderParam; imgSource.Dispose(); return outBmp; } private void compress_Click(object sender, EventArgs e) { SourceImage=SaveImage = GetImageFromStream(Ms);//获取图像 Bitmap bitmap = new Bitmap(SourceImage); SourceImage= GetThumbnail(bitmap, Convert.ToInt32(textBox_SetWidth.Text), Convert.ToInt32(textBox_SetHeight.Text)); //SourceImage=ResizeImage(bitmap, Convert.ToInt32(textBox_SetWidth.Text), Convert.ToInt32(textBox_SetHeight.Text), 0); pictureBox1.Image = SourceImage; pictureBox1.i this.textBox_CurrentWidth.Text = Convert.ToString(SourceImage.Width); this.textBox_CurrentHeight.Text = Convert.ToString(SourceImage.Height); } public Bitmap ResizeImage(Bitmap bmp, int newW, int newH, int Mode) { try { Bitmap b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); // 插值算法的质量 g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch { return null; } } public static Image SaveImage;//保存图像 public static MemoryStream Ms; public static void SaveImageToStream(Image image)//保存图像 { Ms = new MemoryStream(); image.Save(Ms, System.Drawing.Imaging.ImageFormat.Png); } public static Image GetImageFromStream(Stream stream)//获取图像 { MemoryStream ms = stream as MemoryStream; ms.Position = 0; Image image = Image.FromStream(ms); return image; //ms.Close(); } } } |