`
hcmfys
  • 浏览: 347190 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

c# 生成小图

    博客分类:
  • c#
阅读更多
 /// <summary>
    ///
    /// </summary>
    /// <param name="img">图片对象</param>
    /// <param name="fileName">将要保传的文件路径</param>
    /// <param name="targetX">要生成图片的宽度</param>
    /// <param name="targetY">要生成图片的高度<</param>
    private void genreateSmallImage(Bitmap img, string fileName, int targetX, int targetY)
    {
        int w = img.Width;  //当前图片的宽
        int h = img.Height;  //当前图片的高
        int x = 0;           //设置生成图片的宽
        int y = 0;           //设置生成图片的高
        if (w >= targetX && h <= targetY)
        {
            x = targetX;
            y = targetX * h / w;
        }
        else if (h >= targetY && w <= targetX)
        {
            y = targetY;
            x = targetY * w / h;
        }
        else if (h >= targetY && w >= targetX)
        {
            if (h / targetY < w / targetX)
            {
                x = targetX;
                y = targetX * h / w;
            }
            else
            {
                y = targetY;
                x = targetY * w / h;
            }
        }
        else if (h <= targetY && w <= targetX)
        {
            x = img.Width;
            y = img.Height;
        }
        System.Drawing.Image newimg = img.GetThumbnailImage(x, y, null, new System.IntPtr(0));
        newimg.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        img.Dispose();
        newimg.Dispose();
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics