日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

C#实现缩放和剪裁图片的方法示例

發(fā)布時間:2023/12/18 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#实现缩放和剪裁图片的方法示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C#實現(xiàn)縮放和剪裁圖片的方法。分享給大家供大家參考,具體如下:

?
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 using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; namespace Project {class ImageOperation{/// <summary>/// Resize圖片/// </summary>/// <param name="bmp">原始Bitmap </param>/// <param name="newW">新的寬度</param>/// <param name="newH">新的高度</param>/// <param name="Mode">保留著,暫時未用</param>/// <returns>處理以后的圖片</returns>public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH, int Mode){try{Bitmap b = new Bitmap(newW, newH);Graphics g = Graphics.FromImage(b);// 插值算法的質(zhì)量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;}}/// <summary>/// 剪裁 -- 用GDI+/// </summary>/// <param name="b">原始Bitmap</param>/// <param name="StartX">開始坐標X</param>/// <param name="StartY">開始坐標Y</param>/// <param name="iWidth">寬度</param>/// <param name="iHeight">高度</param>/// <returns>剪裁后的Bitmap</returns>public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight){if (b == null){return null;}int w = b.Width;int h = b.Height;if (StartX >= w || StartY >= h){return null;}if (StartX + iWidth > w){iWidth = w - StartX;}if (StartY + iHeight > h){iHeight = h - StartY;}try{Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);Graphics g = Graphics.FromImage(bmpOut);g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);g.Dispose();return bmpOut;}catch{return null;}}} }

目標其實都是new Rectangle(0, 0, iWidth, iHeight),縮放算法把整個原始圖都往目標區(qū)域里塞new Rectangle(0, 0, bmp.Width, bmp.Height),而剪裁只是把原始區(qū)域上等寬等高的那個區(qū)域new Rectangle(StartX, StartY, iWidth, iHeight)1:1的塞到目標區(qū)域里。




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);???????? ????????????// 設置畫布的描繪質(zhì)量???????? ????????????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();????? ????????????// 以下代碼為保存圖片時,設置壓縮質(zhì)量???? ????????????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);
? ? ? ? ? ? // 設置畫布的描繪質(zhì)量 ? ? ? ??
? ? ? ? ? ? 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();
? ? ? ? ? ? // 以下代碼為保存圖片時,設置壓縮質(zhì)量 ? ??
? ? ? ? ? ? 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);
? ? ? ? ? ? ? ? // 插值算法的質(zhì)量
? ? ? ? ? ? ? ? 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();


? ? ? ? }
? ? }
}

總結

以上是生活随笔為你收集整理的C#实现缩放和剪裁图片的方法示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。