码迷,mamicode.com
首页 > 其他好文 > 详细

让图片不变形

时间:2015-11-09 15:22:43      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

  •  #region 让图片不变形         /// <summary>         /// 小图标等比例缩小         /// </summary>         /// <param name="_files"></param>         /// <returns></returns>         private ImageList GetImageFrom2(string[] _files)         {             ImageList list = new ImageList();

                int picWidth;             int picHeight;             int _iconW = 60;             int _iconH = 80;             int _picTop;             int _picLeft;             Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback);             Bitmap myBitmap = null;             Image _tempImage;             for (int i = 0; i < _files.Length; i++)             {

                    myBitmap = new Bitmap(_files[i]);                 CalateSize(myBitmap.Width, myBitmap.Height, _iconW, _iconH, out picWidth, out picHeight);                 Image myThumbnail = myBitmap.GetThumbnailImage(picWidth, picHeight, myCallback, IntPtr.Zero);

                    //第一步                 Bitmap bp = new Bitmap(_iconW, _iconH);                 //第二步                 Graphics g = Graphics.FromImage(bp);                 g.Clear(Color.White);                 g.DrawImage(myThumbnail, (_iconW - picWidth) / 2, (_iconH - picHeight) / 2);                 list.Images.Add(_files[i], (Image)bp);                 list.ImageSize = new Size(_iconW, _iconH);             }             return list;         }         public bool ThumbnailCallback()         {             return false;         }         /// <summary>         /// 获取小图标不变形的大小         /// </summary>         /// <param name="picW"></param>         /// <param name="picH"></param>         /// <param name="iconW"></param>         /// <param name="incoH"></param>         /// <param name="_w"></param>         /// <param name="_h"></param>         public void CalateSize(int picW, int picH, int iconW, int incoH, out int _w, out int _h)         {             int _a = picW;  // 2488             int _b = picH;  // 4288             int _c = iconW;  //60             int _d = incoH;  //80             if ((_a / _b) > (_c / _d))             {                 _w = iconW;  //40                 _h = (_b * iconW / _a);             }             else             {                 _h = incoH;                 _w = (_a * incoH / _b);             }         }

     

            #endregion  让图片不变形

让图片不变形

标签:

原文地址:http://www.cnblogs.com/szlixin/p/4949776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!