• C# 同比缩放图片


    /**
             * 无损缩放图片
             * bitmap 需要缩放的图片
             * w 需要缩放的宽度
             * h 需要缩放的高度
             * */
            public static System.Drawing.Bitmap TBScaleBitmap(System.Drawing.Bitmap bitmap, int w, int h, string mode)
            {
                System.Drawing.Bitmap map = new System.Drawing.Bitmap(w, h);
                System.Drawing.Graphics gra = System.Drawing.Graphics.FromImage(map);
                gra.Clear(System.Drawing.Color.Transparent);//清空画布并以透明背景色填充
                gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿
                gra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gra.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gra.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    
                int towidth = w;
                int toheight = h;
    
                int x = 0;
                int y = 0;
                int ow = bitmap.Width;
                int oh = bitmap.Height;
    
    
    
                switch (mode)
                {
                    case "HW":  //指定高宽缩放(可能变形)                
                        break;
                    case "W":   //指定宽,高按比例                    
                        toheight = bitmap.Height * w / bitmap.Width;
                        break;
                    case "H":   //指定高,宽按比例
                        towidth = bitmap.Width * h / bitmap.Height;
                        break;
                    case "Cut": //指定高宽裁减(不变形)                
                        if ((double)bitmap.Width / (double)bitmap.Height > (double)towidth / (double)toheight)
                        {
                            oh = bitmap.Height;
                            ow = bitmap.Height * towidth / toheight;
                            y = 0;
                            x = (bitmap.Width - ow) / 2;
                        }
                        else
                        {
                            ow = bitmap.Width;
                            oh = bitmap.Width * h / towidth;
                            x = 0;
                            y = (bitmap.Height - oh) / 2;
                        }
                        break;
                    case "MaxHW"://最大宽高比例缩放,比如原100*50->50*30,则结果是50*25
                        var rmaxhw_d1w = bitmap.Width * 1.0 / w;
                        var rmaxhw_d2h = bitmap.Height * 1.0 / h;
                        if (rmaxhw_d1w > rmaxhw_d2h)
                        {
                            if (rmaxhw_d1w <= 1)
                            {
                                towidth = bitmap.Width; h = bitmap.Height;
                                goto case "HW";
                            }
                            towidth = w;
                            goto case "W";
                        }
                        if (rmaxhw_d2h <= 1)
                        {
                            towidth = bitmap.Width; h = bitmap.Height;
                            goto case "HW";
                        }
                        toheight = h;
                        goto case "H";
                    default:
                        break;
                }
    
                gra.DrawImage(bitmap, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
    
                gra.Flush();
                gra.Dispose();
                bitmap.Dispose();
                return map;
            }
  • 相关阅读:
    Tip#66:你知道吗?如何在输入属性值时自动插入双引号
    使用 Apache MINA 开发高性能网络应用程序(转载)
    Faceted Search with Solr
    solr dataimport 数据导入源码分析 补充
    Apache Tika
    MiddlegenHibernate的配制和使用(jtds连接sqlserver数据库)
    汉语转拼音之pinyin4j(转载)
    使用Tika进行非结构化内容的读写1
    使用Java NIO编写高性能的服务器
    solr dataimport 数据导入源码分析(十)总结
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/11125405.html
Copyright © 2020-2023  润新知