zoukankan      html  css  js  c++  java
  • [转]图片处理函数(自适应缩略图datatable中添加缩略图像)

            /// <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;
            }
  • 相关阅读:
    CentOS 7 下Emacs无法录入中文的问题
    GPS文件中的C1--->P1转换
    centos7上搭建http服务器以及设置目录访问
    在Linux和Windows之间的远程控制的实现
    Emacs中的代码折叠控制
    Fortran程序调试中的“吐核”错误
    CentOS 7.6 系统上添加最新版 NetCDF 4.6.1
    迁移 Emacs 的自定义设置
    CentOS 7系统上制作Clonezilla(再生龙)启动U盘并克隆双系统
    [CentOS 7] TexLive2017中kpsewhich Bug的修复
  • 原文地址:https://www.cnblogs.com/oktell/p/4601517.html
Copyright © 2011-2022 走看看