实用开发一:c#封装好的处理图片的水印和生成缩略图的类

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Drawing;
using  System.IO;

/// <summary>
/// ImgWater 的摘要说明
/// </summary>

public   class  ImgWater
{

    
/// <summary>
    
/// 图片水印
    
/// </summary>
    
/// <param name="ImgFile">原图文件地址</param>
    
/// <param name="WaterImg">水印图片地址</param>
    
/// <param name="sImgPath">水印图片保存地址</param>
    
/// <param name="Alpha">水印透明度设置</param>
    
/// <param name="iScale">水印图片在原图上的显示比例</param>
    
/// <param name="intDistance">水印图片在原图上的边距确定,以图片的右边和下边为准,当设定的边距超过一定大小后参数会自动失效</param>

    public void zzsImgWater(
         
string ImgFile
        , 
string WaterImg
        , 
string sImgPath
        , 
float Alpha
        , 
float iScale
        , 
int intDistance
        )
    
{
        
try
        
{

            
//装载图片


            FileStream fs 
= new FileStream(ImgFile, FileMode.Open);
            BinaryReader br 
= new BinaryReader(fs);
            
byte[] bytes = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            MemoryStream ms 
= new MemoryStream(bytes);

            System.Drawing.Image imgPhoto 
= System.Drawing.Image.FromStream(ms);



            
//System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(ImgFile);



            
int imgPhotoWidth = imgPhoto.Width;
            
int imgPhotoHeight = imgPhoto.Height;

            System.Drawing.Image imgWatermark 
= new Bitmap(WaterImg);
            
int imgWatermarkWidth = imgWatermark.Width;
            
int imgWatermarkHeight = imgWatermark.Height;


            
//计算水印图片尺寸

            
decimal aScale = Convert.ToDecimal(iScale);
            
decimal pScale = 0.05M;
            
decimal MinScale = aScale - pScale;
            
decimal MaxScale = aScale + pScale;

            
int imgWatermarkWidthNew = imgWatermarkWidth;
            
int imgWatermarkHeightNew = imgWatermarkHeight;

            
if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth >= imgPhotoHeight)
                
if (imgWatermarkWidth > imgWatermarkHeight)

                    
if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7<= MaxScale))
                    
{

                    }

                    
else
                    
{
                        imgWatermarkWidthNew 
= Convert.ToInt32(imgPhotoWidth * aScale);
                        imgWatermarkHeightNew 
= Convert.ToInt32((imgPhotoWidth * aScale / imgWatermarkWidth) * imgWatermarkHeight);
                    }

                
else
                    
if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7<= MaxScale))
                    
{

                    }

                    
else
                    
{
                        imgWatermarkHeightNew 
= Convert.ToInt32(imgPhotoHeight * aScale);
                        imgWatermarkWidthNew 
= Convert.ToInt32((imgPhotoHeight * aScale / imgWatermarkHeight) * imgWatermarkWidth);
                    }

            
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth >= imgWatermarkHeight)
            
{
                imgWatermarkWidthNew 
= Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew 
= Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }


            
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
            
{
                imgWatermarkWidthNew 
= Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew 
= Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }


            
if (imgWatermarkWidth <= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgPhotoWidth >= imgPhotoHeight)
            
{
                imgWatermarkHeightNew 
= Convert.ToInt32(imgPhotoHeight * aScale);
                imgWatermarkWidthNew 
= Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
            }


            
if (imgPhotoWidth >= imgWatermarkWidth && imgPhotoHeight >= imgWatermarkHeight && imgPhotoWidth <= imgPhotoHeight)
                
if (imgWatermarkWidth > imgWatermarkHeight)
                    
if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkWidth) / Convert.ToDecimal(imgPhotoWidth)), 7<= MaxScale))
                    
{

                    }

                    
else
                    
{
                        imgWatermarkWidthNew 
= Convert.ToInt32(imgPhotoWidth * aScale);
                        imgWatermarkHeightNew 
= Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
                    }

                
else
                    
if ((MinScale <= Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7)) && (Math.Round((Convert.ToDecimal(imgWatermarkHeight) / Convert.ToDecimal(imgPhotoHeight)), 7<= MaxScale))
                    
{

                    }

                    
else
                    
{
                        imgWatermarkHeightNew 
= Convert.ToInt32(imgPhotoHeight * aScale);
                        imgWatermarkWidthNew 
= Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
                    }


            
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight >= imgPhotoHeight && imgWatermarkWidth <= imgWatermarkHeight)
            
{
                imgWatermarkHeightNew 
= Convert.ToInt32(imgPhotoHeight * aScale);
                imgWatermarkWidthNew 
= Convert.ToInt32(((imgPhotoHeight * aScale) / imgWatermarkHeight) * imgWatermarkWidth);
            }


            
if (imgWatermarkWidth >= imgPhotoWidth && imgWatermarkHeight <= imgPhotoHeight && imgPhotoWidth <= imgPhotoHeight)
            
{
                imgWatermarkWidthNew 
= Convert.ToInt32(imgPhotoWidth * aScale);
                imgWatermarkHeightNew 
= Convert.ToInt32(((imgPhotoWidth * aScale) / imgWatermarkWidth) * imgWatermarkHeight);
            }


            
if (imgWatermarkWidth 
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值