[轉]圖片處理函數(自適應縮圖datatable中添加縮圖像)

來源:互聯網
上載者:User

標籤:

        /// <summary>        /// 為DataTable指定行中的產生縮圖        /// </summary>        /// <param name="dataTable">資料來源</param>        /// <param name="dataTableImageColumnName">要產生縮圖的資料來源的列名</param>        /// <param name="appendSmallPicColumnName">新增縮圖資料列的列名</param>        /// <param name="imageFormat">源映像格式</param>        /// <param name="maxWidth">映像自適應的最大寬度</param>        /// <param name="maxHeight">最像自適應的最大高度</param>        public static void GetSmallPic(DataTable dataTable, string dataTableImageColumnName, string appendSmallPicColumnName, ImageFormat imageFormat, int maxWidth, int maxHeight)        {            var dc = new DataColumn(appendSmallPicColumnName, Type.GetType("System.Byte[]"));            dataTable.Columns.Add(dc);            for (int i = 0; i < dataTable.Rows.Count; i++)            {                //產生縮圖GetSmallPic(dataTable.Rows[i], dataTableImageColumnName, appendSmallPicColumnName);                var imageByte = (byte[])dataTable.Rows[i][dataTableImageColumnName];                var ms = new MemoryStream(imageByte, 0, imageByte.Length);                var sourceImage = Image.FromStream(ms);                int newWidth, newHeight;                ImageSelfAdaption(sourceImage, maxWidth, maxHeight, out newWidth, out newHeight);                var myBitmap = new Bitmap(sourceImage, newWidth, newHeight);                ms = new MemoryStream();                myBitmap.Save(ms, imageFormat);                dataTable.Rows[i][appendSmallPicColumnName] = ms.ToArray();                ms.Close();            }        }         /// <summary>        /// 擷取映像自適應後的寬高(設定最大寬高)        /// </summary>        /// <param name="image">映像</param>        /// <param name="maxWidth">最大寬度</param>        /// <param name="maxHeight">最大高度</param>        /// <param name="newWidth">自適應後的映像寬度</param>        /// <param name="newHeight">自適應後的映像高度</param>        /// <returns></returns>        public static void ImageSelfAdaption(Image image, int maxWidth, int maxHeight, out int newWidth, out int newHeight)        {            var originalWidth = image.Width;            var originalHeight = image.Height;            double _newWidth = maxWidth, _newHeight = maxHeight;            double t = originalWidth > maxWidth ? maxWidth : originalWidth;            if (originalHeight * t / originalWidth > maxHeight)            {                _newHeight = maxHeight;                _newWidth = (double)maxHeight / originalHeight * originalWidth;            }            else            {                _newWidth = t;                _newHeight = (t / originalWidth) * originalHeight;            }            newWidth = (int)_newWidth;            newHeight = (int)_newHeight;        }         /// <summary>        /// 擷取補足透明地區的映像        /// </summary>        /// <param name="image">欲補足透明地區的映像</param>        /// <param name="minComplementSize">補足透明地區的最小像素塊大小(註:必須大於等於2)</param>        /// <returns></returns>        public static Bitmap GetComplementImage(Image image, int minComplementSize)        {            if (minComplementSize < 2)            {                return new Bitmap(image.Width, image.Height);            }            else            {                int newWidth = image.Width;                int newHeight = image.Height;                //寬度求餘                int width = image.Width % minComplementSize;                //高度求餘                int height = image.Height % minComplementSize;                //寬度不夠                if (width != 0)                {                    newWidth += minComplementSize - width;                }                if (height != 0) //高度不夠                {                    newHeight += minComplementSize - height;                }                return new Bitmap(image, newWidth, newHeight);            }        }         /// <summary>        /// 擷取映像對象        /// </summary>        /// <param name="imageByte">映像位元據</param>        /// <returns></returns>        public static Image GetImage(byte[] imageByte)        {            if (imageByte != null)            {                var ms = new MemoryStream();                ms.Write(imageByte, 0, imageByte.Length);                return Image.FromStream(ms);            }            return null;        }        /// <summary>        /// 擷取映像位元據        /// </summary>        /// <param name="image">映像</param>        /// <param name="imageFormat">映像格式</param>        /// <returns></returns>        public static byte[] GetImageByteArray(Image image, ImageFormat imageFormat)        {            var ms = new MemoryStream();            image.Save(ms, imageFormat);            var img = new byte[ms.Length];            ms.Position = 0;            ms.Read(img, 0, Convert.ToInt32(ms.Length));            ms.Close();            return img;        }

 

[轉]圖片處理函數(自適應縮圖datatable中添加縮圖像)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.