欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#修改圖片尺寸以及不改變?cè)袌D片比例

 更新時(shí)間:2025年01月07日 15:33:02   作者:syd二寶  
文章介紹了如何使用C#中的Bitmap類來修改圖片尺寸,同時(shí)保持原有的圖片比例,作者分享了自己的經(jīng)驗(yàn),并鼓勵(lì)讀者參考和使用

C#修改圖片尺寸不改變?cè)袌D片比例

C#使用BitMap修改圖片尺寸

修改圖片大小,不改變?cè)袌D片比例

修改后圖片

代碼

public static void image()
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile("圖片路徑");
            Bitmap bt = new Bitmap(img);
            //獲取圖片位置顏色
            Color cl = bt.GetPixel(10, 10);
            int Width = img.Width;
            int Height = img.Height;
            int marginx =  Height / 2;
            float dpiX = img.HorizontalResolution;
            float dpiY = img.VerticalResolution;
            //設(shè)置新圖的大小
            Bitmap bitmap= new Bitmap(Width, Width, PixelFormat.Format24bppRgb);
            //設(shè)置位圖文件的水平和垂直分辨率,與Img一致
            bitmap.SetResolution(dpiX, dpiY);
            //在位圖文件上填充一個(gè)新圖
            Graphics graphics = Graphics.FromImage(bitmap);
            System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(0, 0, Width, Width);
            //定義顏色
            SolidBrush mySolidBrush = new SolidBrush(cl);
            //將新圖填充為獲取原圖位置的顏色
            graphics.FillRectangle(mySolidBrush, Rec);
            //向新圖中填充Img
            graphics.DrawImage(img, 0, marginx, Rec, GraphicsUnit.Pixel);
            graphics.Dispose();
            GC.Collect();
            bitmap.Save("保存圖片路徑", System.Drawing.Imaging.ImageFormat.Jpeg);
        }

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論