图片管理

    public   class ImageUtils
    {
        /// <summary>
        ///字节数组转换成图片
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static Image ByteToImage(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            ms.Position = 0;
            Image img = Image.FromStream(ms);
            ms.Close();
            return img;
        }
        /// <summary>
        /// 根椐获取网络图片
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private static Image GetImageByUrl(string url)
        {
            Image image = null;
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //加上这一句
                image = Image.FromStream(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream());
            }
            catch (Exception ex)
            {
                image = null;
            }
            return image;
        }

        /// <summary>
        /// 获取本地图片
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private static Image GetImageByFileName(string filename)
        {
            Image image = null;
            try
            {
                if (File.Exists(filename))
                {
                    image = Image.FromFile(filename);
                }
            }
            catch (Exception ex)
            {
                image = null;
            }
            return image;
        }

        // 按比例缩放图片
        public static Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight)
        {
            int IntWidth; //新的图片宽
            int IntHeight; //新的图片高
            try
            {


                //计算缩放图片的大小 http://www.cnblogs.com/roucheng/

                if (SourceImage.Width > TargetWidth && SourceImage.Height <= TargetHeight)//宽度比目的图片宽度大,长度比目的图片长度小
                {
                    IntWidth = TargetWidth;
                    IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
                }
                else if (SourceImage.Width <= TargetWidth && SourceImage.Height > TargetHeight)//宽度比目的图片宽度小,长度比目的图片长度大
                {
                    IntHeight = TargetHeight;
                    IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
                }
                else if (SourceImage.Width <= TargetWidth && SourceImage.Height <= TargetHeight) //长宽比目的图片长宽都小
                {
                    IntHeight = SourceImage.Width;
                    IntWidth = SourceImage.Height;
                }
                else//长宽比目的图片的长宽都大
                {
                    IntWidth = TargetWidth;
                    IntHeight = (IntWidth * SourceImage.Height) / SourceImage.Width;
                    if (IntHeight > TargetHeight)//重新计算
                    {
                        IntHeight = TargetHeight;
                        IntWidth = (IntHeight * SourceImage.Width) / SourceImage.Height;
                    }
                }

                System.Drawing.Imaging.ImageFormat format = SourceImage.RawFormat;
                System.Drawing.Bitmap SaveImage = new System.Drawing.Bitmap(IntWidth, IntHeight);
                Graphics g = Graphics.FromImage(SaveImage);
                g.Clear(Color.White);
                g.DrawImage(SourceImage, 0, 0, IntWidth, IntHeight);
                SourceImage.Dispose();

                return SaveImage;
            }
            catch (Exception ex)
            {

            }

            return null;
        }

    }

 

posted on 2020-05-18 13:38  高达  阅读(135)  评论(0)    收藏  举报

导航