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;
default: break;
}
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(0, 0, 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();
}
}
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;
default: break;
}
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(0, 0, 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();
}
}
分类:
03~c#
, 020~asp.net
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 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)