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

该博客介绍了如何使用C#进行图片处理,包括生成新图片名称、保存图片、创建缩略图以及添加水印。通过示例代码展示了如何使用ImgWater类来调整图片大小、添加水印效果,并提供了最终效果的展示链接。
摘要由CSDN通过智能技术生成
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 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值