zoukankan      html  css  js  c++  java
  • 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;
            }
        }

  • 相关阅读:
    文本属性和字体属性
    BZOJ 2724: [Violet 6]蒲公英
    BZOJ4010: [HNOI2015]菜肴制作
    BZOJ 2160: 拉拉队排练
    HDU-5157Harry and magic string
    HDU-5421Victor and String
    BZOJ2565: 最长双回文串(回文树)
    BZOJ3676: [Apio2014]回文串(回文树)
    BZOJ 3195: [Jxoi2012]奇怪的道路(状压dp)
    BZOJ1758: [Wc2010]重建计划(01分数规划+点分治+单调队列)
  • 原文地址:https://www.cnblogs.com/cuihongyu3503319/p/1353545.html
Copyright © 2011-2022 走看看