栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

c#生成自定义图片方法代码实例

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c#生成自定义图片方法代码实例

本篇文章给大家带来的内容是关于c# 如何生成自定义图片?c# 生成自定义图片方法,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

using System.Drawing;using System.IO;using System.Drawing.Imaging;using System;namespace treads
{  /// 
  /// 生成略缩图  /// 
  public class Class2
  {    private Image srcImage;    private string srcFileName= @"X";//获取图片的路径
    private string srcFileName1 = @"x";//要保持图片的新路径
 
    /// 
    /// 回调    /// 
    /// 
    public bool ThumbnailCallback()
    {      return false;
    }    /// 
    /// 保存缩略图    /// 
    /// 
    /// 
    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;
      }
    }    /// 
    /// 生成缩略图并保存    /// 
    /// 缩略图的宽度
    /// 缩略图的高度
    /// 保存的图像格式
    /// 缩略图的Image对象
    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();
      }
    }
 
  }
}

制作网络下载的略缩图

/// 
   /// 制作远程缩略图    /// 
   /// 图片URL
   /// 新图路径
   /// 最大宽度
   /// 最大高度
   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);
   }  /// 
   /// 获取图片流    /// 
   /// 图片URL
   /// 
   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;
     }
   } /// 
   /// 裁剪图片并保存    /// 
   /// 源图路径(绝对路径)
   /// 缩略图路径(绝对路径)
   /// 缩略图宽度
   /// 缩略图高度
   /// 裁剪宽度
   /// 裁剪高度
   /// X轴
   /// Y轴
   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# 生成自定义图片方法的全部介绍,感谢大家对考高分网的支持。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122789.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号