随笔 - 1330  文章 - 1  评论 - 378  阅读 - 482万 
复制代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;

public class PictureSlightly
{
    
public PictureSlightly()
    {
        
//
        
// TODO: Add constructor logic here
        
//
    }
    
/// <summary>
    
/// 生成缩略图
    
/// </summary>
    
/// <param name="SourceImage">源图路径(含图片,物理路径)</param>
    
/// <param name="ImageMapth">缩略图路径(含图片,物理路径)</param>
    
/// <param name="width">缩略图宽</param>
    
/// <param name="height">缩略图高</param>
    public static void MakeImage(string sourceImage, string imageMapth, int width, int height, string mode)
    {
        System.Drawing.Image MyImage 
= System.Drawing.Image.FromFile(sourceImage);
        
int towidth = width;
        
int toheight = height;
        
int x = 0, y = 0;
        
int ow = MyImage.Width;
        
int oh = MyImage.Height;
        
switch (mode)
        {
            
case "hw"break;
            
case "w": toheight = MyImage.Height * width / MyImage.Width; break;
            
case "h": towidth = MyImage.Width * height / MyImage.Height; break;
            
case "cut":
                
if ((double)MyImage.Width / (double)MyImage.Height > (double)towidth / (double)toheight)
                {
                    oh 
= MyImage.Height;
                    ow 
= MyImage.Width * towidth / toheight;
                    y 
= 0;
                    x 
= (MyImage.Width - ow) / 2;
                }
                
else
                {
                    ow 
= MyImage.Width;
                    oh 
= MyImage.Height * height / toheight;
                    x 
= 0;
                    y 
= (MyImage.Height - oh) / 2;
                }; 
break;
            
defaultbreak;
        }

        System.Drawing.Image bitmap 
= new System.Drawing.Bitmap(towidth, toheight);
        Graphics g 
= Graphics.FromImage(bitmap);
        g.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.High;
        g.SmoothingMode 
= System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.Clear(Color.Transparent);
        g.DrawImage(MyImage, 
new Rectangle(00, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
        
try
        {
            bitmap.Save(imageMapth, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        
catch
        {
        }
        MyImage.Dispose();
        bitmap.Dispose();
        g.Dispose();
    }
}
复制代码
posted on   钱途无梁  阅读(429)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示