• netcore 图片缩略图


           /// <summary>
            /// 取小写文件名后缀
            /// </summary>
            /// <param name="name">文件名</param>
            /// <returns>返回小写后缀,不带“.”</returns>
            public static string GetFileExt(string name)
            {
                return name.Split(".").Last().ToLower();
            }
    
            /// <summary>
            /// 是否为图片文件
            /// </summary>
            /// <param name="fileExt">文件扩展名,不含“.”</param>
            public static bool IsImage(string fileExt)
            {
                ArrayList al = new ArrayList { "bmp", "jpeg", "jpg", "gif", "png", "ico" };
                return al.Contains(fileExt);
            }
    
            /// <summary>
            /// 检查是否允许文件
            /// </summary>
            /// <param name="fileExt">文件后缀</param>
            /// <param name="allowExt">允许文件数组</param>
            public static bool CheckFileExt(string fileExt, string[] allowExt)
            {
                return allowExt.Any(t => t == fileExt);
            }
    
    
            /// <summary>
            /// 制作缩略图
            /// </summary>
            /// <param name="original">图片对象</param>
            /// <param name="newFileName">新图路径</param>
            /// <param name="maxWidth">最大宽度</param>
            /// <param name="maxHeight">最大高度</param>
            public static void ThumbImg(Image original, string newFileName, int maxWidth, int maxHeight)
            {
                Size newSize = ResizeImage(original.Width, original.Height, maxWidth, maxHeight);
                using (Image displayImage = new Bitmap(original, newSize))
                {
                    try
                    {
                        displayImage.Save(newFileName, original.RawFormat);
                    }
                    finally
                    {
                        original.Dispose();
                    }
                }
            }
    
            /// <summary>
            /// 制作缩略图
            /// </summary>
            /// <param name="fileName">文件名</param>
            /// <param name="newFileName">新图路径</param>
            /// <param name="maxWidth">最大宽度</param>
            /// <param name="maxHeight">最大高度</param>
            public static void ThumbImg(string fileName, string newFileName, int maxWidth, int maxHeight)
            {
                byte[] imageBytes = File.ReadAllBytes(fileName);
                Image img = Image.FromStream(new MemoryStream(imageBytes));
                ThumbImg(img, newFileName, maxWidth, maxHeight);
            }
    
    
            /// <summary>
            /// 计算新尺寸
            /// </summary>
            /// <param name="width">原始宽度</param>
            /// <param name="height">原始高度</param>
            /// <param name="maxWidth">最大新宽度</param>
            /// <param name="maxHeight">最大新高度</param>
            /// <returns></returns>
            private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
            {
                if (maxWidth <= 0)
                    maxWidth = width;
                if (maxHeight <= 0)
                    maxHeight = height;
                decimal MAX_WIDTH = maxWidth;
                decimal MAX_HEIGHT = maxHeight;
                decimal ASPECT_RATIO = MAX_WIDTH / MAX_HEIGHT;
    
                int newWidth, newHeight;
                decimal originalWidth = width;
                decimal originalHeight = height;
    
                if (originalWidth > MAX_WIDTH || originalHeight > MAX_HEIGHT)
                {
                    decimal factor;
                    if (originalWidth / originalHeight > ASPECT_RATIO)
                    {
                        factor = originalWidth / MAX_WIDTH;
                        newWidth = Convert.ToInt32(originalWidth / factor);
                        newHeight = Convert.ToInt32(originalHeight / factor);
                    }
                    else
                    {
                        factor = originalHeight / MAX_HEIGHT;
                        newWidth = Convert.ToInt32(originalWidth / factor);
                        newHeight = Convert.ToInt32(originalHeight / factor);
                    }
                }
                else
                {
                    newWidth = width;
                    newHeight = height;
                }
                return new Size(newWidth, newHeight);
            }
    
    
            /// <summary>
            /// 得到图片格式
            /// </summary>
            /// <param name="name">文件名称</param>
            /// <returns></returns>
            public static ImageFormat GetFormat(string name)
            {
                string ext = GetFileExt(name);
                switch (ext)
                {
                    case "ico":
                        return ImageFormat.Icon;
                    case "bmp":
                        return ImageFormat.Bmp;
                    case "png":
                        return ImageFormat.Png;
                    case "gif":
                        return ImageFormat.Gif;
                    default:
                        return ImageFormat.Jpeg;
                }
            }

    报错

    2019-05-09 10:27:01,330 线程ID:[80] 日志级别:ERROR 出错类:WebApp.HttpGlobalExceptionFilter property:[(null)] - 错误描述:System.TypeInitializationException: The type initializer for 'System.DrawingCore.GDIPlus' threw an exception. ---> System.DllNotFoundException: Unable to load shared library 'gdiplus' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libgdiplus: cannot open shared object file: No such file or directory
       at System.DrawingCore.GDIPlus.GdiplusStartup(UInt64& token, GdiplusStartupInput& input, GdiplusStartupOutput& output)
       at System.DrawingCore.GDIPlus..cctor()
       --- End of inner exception stack trace ---
       at System.DrawingCore.GDIPlus.GdipGetGenericFontFamilySansSerif(IntPtr& fontFamily)
       at System.DrawingCore.FontFamily..ctor(GenericFontFamilies genericFamily)
       at System.DrawingCore.FontFamily.get_GenericSansSerif()
       at System.DrawingCore.Font.CreateFont(String familyName, Single emSize, FontStyle style, GraphicsUnit unit, Byte charSet, Boolean isVertical)
       at Common.VerifyCodeHelper.CreateByteByImgVerifyCode(String verifyCode, Int32 width, Int32 height) in F:srcWebAppCommonVerifyCodeHelper.cs:line 206
       at WebApp.Controllers.VerifyCodeController.NumberVerifyCode() in F:srcWebAppControllersVerifyCodeController.cs:line 27
       at lambda_method(Closure , Object , Object[] )
       at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
       at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()

    解决方法

    Centos 7

    yum install libgdiplus-devel

  • 相关阅读:
    vscode conda 配置python环境(windows)
    Linux分区只能分两个,无法安装双系统(解决)
    离散傅里叶变换,逆变换(c语言)
    vscode 配置task.json,执行多条指令
    cmake出错:CMAKE_CXX_COMPILER设置后,提示没有设置,找不到make命令的可执行程序
    产生makefiles文件后,make命令不可用
    Kali 下载地址
    vc6 保存文件卡住
    fatal error LNK1169: one or more multiply defined symbols found
    Google Chrome 退出清除浏览数据
  • 原文地址:https://www.cnblogs.com/chenyishi/p/12302058.html
Copyright © 2020-2023  润新知