溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

如何在c#中生成自定義圖片

發(fā)布時(shí)間:2020-07-14 10:36:00 來(lái)源:億速云 閱讀:243 作者:Leah 欄目:編程語(yǔ)言

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何在c#中生成自定義圖片,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

using System.Drawing;using System.IO;using System.Drawing.Imaging;using System;namespace treads
{    /// <summary>
    /// 生成略縮圖    /// </summary>
    public class Class2
    {        private Image srcImage;        private string srcFileName= @"X";//獲取圖片的路徑
        private string srcFileName1 = @"x";//要保持圖片的新路徑

        /// <summary>
        /// 回調(diào)        /// </summary>
        /// <returns></returns>
        public bool ThumbnailCallback()
        {            return false;
        }        /// <summary>
        /// 保存縮略圖        /// </summary>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        public void SaveThumbnailImage(int Width, int Height)
        {            switch (Path.GetExtension(srcFileName).ToLower())
            {                case ".png":
                    SaveImage(Width, Height, ImageFormat.Png);                    break;                case ".gif":
                    SaveImage(Width, Height, ImageFormat.Gif);                    break;                default:
                    SaveImage(Width, Height, ImageFormat.Jpeg);                    break;
            }
        }        /// <summary>
        /// 生成縮略圖并保存        /// </summary>
        /// <param name="Width">縮略圖的寬度</param>
        /// <param name="Height">縮略圖的高度</param>
        /// <param name="imgformat">保存的圖像格式</param>
        /// <returns>縮略圖的Image對(duì)象</returns>
        public void SaveImage(int Width, int Height, ImageFormat imgformat)
        {
            srcImage = Image.FromFile(srcFileName);            if (imgformat != ImageFormat.Gif && (srcImage.Width > Width) || (srcImage.Height > Height))
            {
                Image img;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                img = srcImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
                srcImage.Dispose();
                img.Save(srcFileName1, imgformat);
                img.Dispose();
            }
        }

    }
}

制作網(wǎng)絡(luò)下載的略縮圖

  /// <summary>
        /// 制作遠(yuǎn)程縮略圖        /// </summary>
        /// <param name="url">圖片URL</param>
        /// <param name="newFileName">新圖路徑</param>
        /// <param name="maxWidth">最大寬度</param>
        /// <param name="maxHeight">最大高度</param>
        public static void MakeRemoteThumbnailImage(string url, string newFileName, int maxWidth, int maxHeight)
        {
            Stream stream = GetRemoteImage(url);            if (stream == null)                return;
            Image original = Image.FromStream(stream);
            stream.Close();
            MakeThumbnailImage(original, newFileName, maxWidth, maxHeight);
        }   /// <summary>
        /// 獲取圖片流        /// </summary>
        /// <param name="url">圖片URL</param>
        /// <returns></returns>
        private static Stream GetRemoteImage(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.ContentLength = 0;
            request.Timeout = 20000;
            HttpWebResponse response = null;            try
            {
                response = (HttpWebResponse)request.GetResponse();                return response.GetResponseStream();
            }            catch
            {                return null;
            }
        } /// <summary>
        /// 裁剪圖片并保存        /// </summary>
        /// <param name="fileName">源圖路徑(絕對(duì)路徑)</param>
        /// <param name="newFileName">縮略圖路徑(絕對(duì)路徑)</param>
        /// <param name="maxWidth">縮略圖寬度</param>
        /// <param name="maxHeight">縮略圖高度</param>
        /// <param name="cropWidth">裁剪寬度</param>
        /// <param name="cropHeight">裁剪高度</param>
        /// <param name="X">X軸</param>
        /// <param name="Y">Y軸</param>
        public static bool MakeThumbnailImage(string fileName, string newFileName, int maxWidth, int maxHeight, int cropWidth, int cropHeight, int X, int Y)
        {            byte[] imageBytes = File.ReadAllBytes(fileName);
            Image originalImage = Image.FromStream(new System.IO.MemoryStream(imageBytes));
            Bitmap b = new Bitmap(cropWidth, cropHeight);            try
            {                using (Graphics g = Graphics.FromImage(b))
                {                    //設(shè)置高質(zhì)量插值法
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;                    //設(shè)置高質(zhì)量,低速度呈現(xiàn)平滑程度
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;                    //清空畫(huà)布并以透明背景色填充                    g.Clear(Color.Transparent);                    //在指定位置并且按指定大小繪制原圖片的指定部分
                    g.DrawImage(originalImage, new Rectangle(0, 0, cropWidth, cropHeight), X, Y, cropWidth, cropHeight, GraphicsUnit.Pixel);
                    Image displayImage = new Bitmap(b, maxWidth, maxHeight);
                    SaveImage(displayImage, newFileName, GetCodecInfo("image/" + GetFormat(newFileName).ToString().ToLower()));                    return true;
                }
            }            catch (System.Exception e)
            {                throw e;
            }            finally
            {
                originalImage.Dispose();
                b.Dispose();
            }
        }

上述就是小編為大家分享的如何在c#中生成自定義圖片了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI