您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
怎么在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#是一个简单、通用、面向对象的编程语言,它由微软Microsoft开发,继承了C和C++强大功能,并且去掉了一些它们的复杂特性,C#综合了VB简单的可视化操作和C++的高运行效率,以其强大的操作能力、优雅的语法风格、创新的语言特性和便捷的面向组件编程从而成为.NET开发的首选语言,但它不适用于编写时间急迫或性能非常高的代码,因为C#缺乏性能极高的应用程序所需要的关键功能。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。