欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*****************************************************************
 * 创建人:sunwugang
 * 主要作用:图片处理类
 * 1、生成缩略图片或按照比例改变图片的大小和画质 
 * 2、将生成的缩略图放到指定的目录下 
 *  创建日期:2018-07-27
 *****************************************************************/
namespace KK.TriageSys.Helper
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
 
    public class ImageHelper
    {
        private static ImageHelper instance;
        public static ImageHelper Instance
        {
            get
            {
                if (instance == null) instance = new ImageHelper();
                return ImageHelper.instance;
            }
        }
 
        //图片宽
        private int ImageWidth;
        /// <summary>
        /// 图片高
        /// </summary>
        private int ImageHeight;
 
        /// <summary>  
        /// 根据文件路径获取图片对象  
        /// </summary>  
        /// <param name="ImageFileName">图片文件的全路径名称</param>  
        public Image GetImageByPath(string ImageFileName)
        {
            return Image.FromFile(ImageFileName);
        }
 
        public bool ThumbnailCallback()
        {
            return false;
        }
 
        /// <summary>  
        /// 生成缩略图重载方法1,返回缩略图的Image对象  
        /// </summary>  
        /// <param name="img">图片对象</param>  
        /// <param name="Width">缩略图的宽度</param>  
        /// <param name="Height">缩略图的高度</param>  
        /// <returns>缩略图的Image对象</returns>  
        public Image GetReducedImage(Image img,int Width, int Height)
        {
            try
            {
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                return img.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                LogHelper.Default.SaveText("ImageHelper  GetReducedImage:" + ex.Message + ex.StackTrace);
                return null;
            }
        }
 
        /// <summary>  
        /// 生成缩略图重载方法2,将缩略图文件保存到指定的路径  
        /// </summary>  
        /// <param name="img">图片对象</param>  
        /// <param name="Width">缩略图的宽度</param>  
        /// <param name="Height">缩略图的高度</param>  
        /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>  
        /// <returns>成功返回true,否则返回false</returns>  
        public bool GetReducedImage(Image img, int Width, int Height, string targetFilePath)
        {
            try
            {
                Image ReducedImage;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                ReducedImage = img.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
                ReducedImage.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Default.SaveText("ImageHelper  GetReducedImage:" + ex.Message + ex.StackTrace);
                return false;
            }
        }
 
        /// <summary>  
        /// 生成缩略图重载方法3,返回缩略图的Image对象  
        /// </summary>  
        /// <param name="img">图片对象</param>  
        /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>    
        /// <returns>缩略图的Image对象</returns>  
        public Image GetReducedImage(Image img, double Percent)
        {
            try
            {
                Image ReducedImage;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                ImageWidth = Convert.ToInt32(img.Width * Percent);
                ImageHeight = Convert.ToInt32(img.Width * Percent);
                ReducedImage = img.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
                return ReducedImage;
            }
            catch (Exception ex)
            {
                LogHelper.Default.SaveText("ImageHelper  GetReducedImage:" + ex.Message + ex.StackTrace);
                return null;
            }
        }
 
        /// <summary>  
        /// 生成缩略图重载方法4,返回缩略图的Image对象  
        /// </summary>  
        /// <param name="img">图片对象</param>  
        /// <param name="Percent">缩略图的宽度百分比 如:需要百分之80,就填0.8</param>    
        /// <param name="targetFilePath">缩略图保存的全文件名,(带路径),参数格式:D:Images ilename.jpg</param>  
        /// <returns>成功返回true,否则返回false</returns>  
        public bool GetReducedImage(Image img, double Percent, string targetFilePath)
        {
            try
            {
                Image ReducedImage;
                Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
                ImageWidth = Convert.ToInt32(img.Width * Percent);
                ImageHeight = Convert.ToInt32(img.Width * Percent);
                ReducedImage = img.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);
                ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
                ReducedImage.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Default.SaveText("ImageHelper  GetReducedImage:" + ex.Message + ex.StackTrace);
                return false;
            }
        }
 
    }
}

  

posted on   sunwugang  阅读(232)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示