旗下导航:搜·么
当前位置:网站首页 > .Net教程 > 正文

c# 怎样生成自定义图片?c# 生成自定义图片要领【C#.Net教程】,c#生成自定义图片,方法

作者:搜教程发布时间:2019-11-27分类:.Net教程浏览:65评论:0


导读:本篇文章给人人带来的内容是关于c#怎样生成自定义图片?c#生成自定义图片要领,有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。usingSystem.D...
本篇文章给人人带来的内容是关于c# 怎样生成自定义图片?c# 生成自定义图片要领,有肯定的参考价值,有须要的朋侪能够参考一下,愿望对你有所协助。

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>
        /// 回调        /// </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对象</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();
            }
        }

    }
}

制造收集下载的略缩图

  /// <summary>
        /// 制造长途缩略图        /// </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">源图途径(绝对途径)</param>
        /// <param name="newFileName">缩略图途径(绝对途径)</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))
                {                    //设置高质量插值法
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;                    //设置高质量,低速率显现腻滑水平
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;                    //清空画布并以通明背景色添补                    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# 怎样生成自定义图片?c# 生成自定义图片要领的悉数引见,假如您想相识更多有关C++视频教程,请关注ki4网。

以上就是c# 怎样生成自定义图片?c# 生成自定义图片要领的细致内容,更多请关注ki4网别的相干文章!

标签:c#生成自定义图片方法


欢迎 发表评论: