• 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;
            }
  • 相关阅读:
    U-Learning服务端
    C# 向txt文件中写入
    二维码生成 Gma.QrCodeNet (目前测试支持.net4.0及以上,但vs版本2010不可以 NuGet中搜索不到程序包)
    数据显示按规格向datatable中增加空白记录
    sql server 查询出整数 (可灵活运用)
    sql server 列字段拼接 —— STUFF
    layui confirm 嵌套使用 (随笔记)
    sql server 随记 -- 月份/日期 查询
    SQL Server 数据库备份语句
    关于ScriptManager.RegisterStartupScript 摘录
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/11125405.html
Copyright © 2020-2023  润新知