bs和cs缩放图片

   以前写过这个内容,但是找不到了再写一次 由于cs和bs 显示图片原理不一样,所以不一样

(bs显示原理图片为生成图片输出到客户端,当然不包括客户端的绘图程序VML,SVG等,而cs程序界面本身就可以看作是一个画板)

(bs显示图片除了image控件,直接输出http流也行,但是如果要用image控件,就必须依赖image.url也就是bs的image控件必须依赖一个存在的物理地址显示而不像cs的控件可以依赖一个内存流,不管是图片,.aspx,.ashx等等,也就是想要动态显示图片需要在链接地址或者流输出上做文章)

    /// <summary>
    /// 缩放图片(cs控件用,因为cs控件需要返回System.Drawing.Image)原理需要生成新图片宽度
    /// </summary>
    /// <param name="img">原图片</param>
    /// <param name="xWith">缩放宽比例,如果想缩小图片,小于100</param>
    /// <param name="yHeight">缩放高比例</param>
    /// <returns>返回处理后图片</returns>
    public System.Drawing.Image scaleImg(System.Drawing.Image img, int xWith, int yHeight)
    {
        //计算处理后图片宽
        int i = Convert.ToInt32(img.Width * xWith / 100);
        //计算处理后图片高
        int j = Convert.ToInt32(img.Height * yHeight / 100);
        //格式化图片
        System.Drawing.Image imgScale = new System.Drawing.Bitmap(i, j, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(imgScale);
        System.Drawing.Rectangle srcRect = new System.Drawing.Rectangle(0, 0, img.Width, img.Height);
        System.Drawing.Rectangle desRect = new System.Drawing.Rectangle(0, 0, imgScale.Width, imgScale.Height);
        g.Clear(System.Drawing.Color.White);
        g.DrawImage(img, desRect, srcRect, System.Drawing.GraphicsUnit.Pixel);
        //处理后的图片另存
        imgScale.Save("E:\\1111.jpg", System.Drawing.Imaging.ImageFormat.Gif);
        g.Dispose();
        return imgScale;
    }

    /// <summary>
    /// bs用,按比例缩小图片(高度按缩小按宽度的缩小比例缩小)原理直接控制控件宽度
    /// </summary>
    /// <param name="ImageMap">界面显示的Image</param>
    /// <param name="ImagePath">图片虚拟路径</param>
    /// <param name="WidthSize">图片缩小后定下的宽度</param>
    /// <param name="Server">本页的Server</param>
    public void ImageSize(System.Web.UI.WebControls.Image ImageMap, string ImagePath, int WidthSize, System.Web.HttpServerUtility Server)
    {
        System.Drawing.Image Img = System.Drawing.Image.FromFile(Server.MapPath(ImagePath));
        int ImgWidth = Img.Width;
        int ImgHeight = Img.Height;
        ImageMap.ImageUrl = ImagePath;
        if (ImgWidth > WidthSize)
        {
            ImageMap.Width = WidthSize;
            ImageMap.Height = WidthSize * ImgHeight / ImgWidth;
        }
    }

原文地址:https://www.cnblogs.com/cuihongyu3503319/p/1353545.html