zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    Extjs4循序渐进(二)——Ext的界面(容器和布局)
    Extjs4循序渐进(一)——开始Ext
    重写jQuery的$字符
    WinForm窗口基础配置
    C# 遍历文件夹图片并用ListView控件展示
    Extjs4循序渐进(三)——表单及表单控件详解一(表单布局和基础控件 Text,TextArea,Number,Checkbox,Radio,Date)
    【ezj】一款国产JavaScript 框架,使用体验
    使用Java语言实现几种常见的排序算法
    http://www4.it168.com/jtzt/shenlan/tech/netdesignp/
    写在前面的话
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/11125405.html
Copyright © 2011-2022 走看看