asp.net生成缩略图及给原始图加水印和版权信息

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;


namespace A8.Watermark
{
 /// <summary>
 ///将上传的图片增加水印及生成缩略图
 /// </summary>
 public class watermark
 {
  /// <summary>
  /// 水印图
  /// </summary>
  public Bitmap LogoImage
  {
   get{return this._logoImage;}
  }
  /// <summary>
  /// 缩略图
  /// </summary>
  public Bitmap ThumbImage
  {
   get{return this._thumbImage;}
  }
  /// <summary>
  /// 叠加在图片上的文字
  /// </summary>
  public string TextImage
  {
   get{return this._textImage;}
   set{this._textImage=value ;}
  }
       /// <summary>
  /// 上传的图片流
  /// </summary>
  public Stream Upstream
  {
   get{ return this._upstream;}
   set{this._upstream = value;}
  }

  public string LogoFile
  {
   get{ return this._logoFile;}
   set{this._logoFile = value;}
  }  
  public int PicWidth
  {
   get{ return this._picwidth;}   
  }


  private Bitmap _logoImage;
  private Bitmap _thumbImage;
  private string _textImage;
  private Stream _upstream;
  
  private string _logoFile;
  private int _picwidth;

        /// <summary>
        /// 生成水印图
        /// </summary>
        /// <param name="txtImage">版权信息</param>
        /// <param name="upstream">上传文件流</param>
        /// <param name="logofile">水印图Logo的绝对路径</param>
  public watermark(string txtImage,Stream upstream,string logofile)
  {
     this.TextImage = txtImage;
     this.Upstream = upstream;
     this._logoFile = logofile;
     AddLogoPic();    
  }
        /// <summary>
        /// 生成水印图和缩略图
        /// </summary>
        /// <param name="txtImage">版权信息</param>
        /// <param name="upstream">上传文件流</param>
        /// <param name="logofile">水印图Logo的绝对路径</param>
        /// <param name="picwidth">缩略图的大小</param>
  public watermark(string txtImage,Stream upstream,string logofile,int picwidth)
  {
   this.TextImage = txtImage;
   this.Upstream = upstream;
   this._logoFile = logofile;
   this._picwidth = picwidth;
   AddLogoPic();
   BuildThumbPic();
  }

        /// <summary>
        /// 加水印
        /// </summary>
  private void AddLogoPic()
  {   
   try
   {
     Image upimage = Image.FromStream(this.Upstream);
     Image simage = Image.FromFile(this.LogoFile);

              //logo上填充文字
              Bitmap bitlogo=new Bitmap(simage);
     Graphics sg= Graphics.FromImage(bitlogo);
     Font textFont = new Font("arial",8);
     RectangleF rectangle = new RectangleF(0,51,simage.Width,14);
     StringFormat StrFormat = new StringFormat();
     StrFormat.Alignment = StringAlignment.Far;

     sg.DrawString(this.TextImage,textFont,new SolidBrush(Color.FromArgb(153, 0, 0, 0)), rectangle,StrFormat);
     SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255)); //设透明字体
     sg.DrawString(this.TextImage,textFont,semiTransBrush, rectangle,StrFormat);
     sg.Flush();    

     Bitmap bitimage = new Bitmap(upimage);    
     Graphics g = Graphics.FromImage(bitimage);
     g.DrawImage(bitlogo,new Rectangle(upimage.Width-simage.Width,upimage.Height-simage.Height,upimage.Width,upimage.Height),0,0,upimage.Width,upimage.Height,GraphicsUnit.Pixel);
     g.Flush();
     this._logoImage =bitimage;
     bitlogo.Dispose();
     upimage.Dispose();
     simage.Dispose();
   
   }
   catch(Exception ex)
   {
     throw ex;
   }
  }
  /// <summary>
  /// 生成缩略图
  /// </summary>
  private void BuildThumbPic()
  {
   try
   { int width,height,newwidth,newheight;
    Image upimage = Image.FromStream(this.Upstream);
    Image simage = Image.FromFile(this.LogoFile);
    width = upimage.Width;
    height = upimage.Height;
    if(width>height)
    {
     newwidth=this.PicWidth;
     newheight =(int)((double)height/(double)width * (double)newwidth);
    }
    else
    {
     newheight=this.PicWidth;
     newwidth=(int)((double)width/(double)height * (double)newheight);   
    }
    if (newwidth <= 0)
     throw new ArgumentOutOfRangeException("width", newwidth, "缩略图片宽度不能为0");
    if (newheight <= 0)
     throw new ArgumentOutOfRangeException("height", newheight, "缩略图片高度不能为0");

    this._thumbImage =new Bitmap(upimage.GetThumbnailImage(newwidth,newheight,null,IntPtr.Zero));
    upimage.Dispose();
    simage.Dispose();
   }
   catch(Exception ex)
   {
      throw ex;
   } 
   
  }

  ~watermark()
  {
   Dispose(false);
  }
  
  public void Dispose()
  {
   GC.SuppressFinalize(this);
   this.Dispose(true);
  }
  protected virtual void Dispose(bool disposing)
  {
   if (disposing) 
   {
    if(this._logoImage!=null)
    this._logoImage.Dispose();
    if(this._thumbImage!=null)
    this._thumbImage.Dispose();
   }
  }

  

 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ASP.NET Core MVC 是一种基于 .NET Core 的 web 框架,它不仅支持常见的 web 应用程序模式,如 MVC (Model-View-Controller) 和 API (Application Programming Interface),还提供了一系列集成工具和插件,方便开发人员进行 web 应用程序的快速开发。 在 ASP.NET Core MVC 中,图片上传方法可以通过将表单中的文件数据流存储在服务器上来实现。可以使用 IFormFile 接口和相应的绑定器来读取表单中的文件数据流。以下是一个示例代码片段,演示了如何上传并处理单个图片: public async Task<IActionResult> UploadImage(List<IFormFile> files) { long size = files.Sum(f => f.Length); foreach (var formFile in files) { if (formFile.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse (formFile.ContentDisposition).FileName.Trim('"'); var filePath = @"C:\temp\uploads\" + fileName; using (var stream = new FileStream(filePath, FileMode.Create)) { await formFile.CopyToAsync(stream); } } } return Ok(new { count = files.Count, size }); } 代码中,我们首先将上传的文件存储在指定的目录中,然后返回一个包含上传的文件数量和总文件大小的 JSON 数据。这个过程可以通过简单的编辑和适当的修改快速适用于你的图片上传需求。 总的来说,ASP.NET Core MVC 提供了强大的功能和灵活性,使得开发人员能够轻松地构建和维护复杂的 web 应用程序。针对图片上传,使用 IFormFile 接口和相应的处理器,可以方便地实现图片上传的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fys

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值