netcore picture thumbnail

       ///  <Summary> 
        /// taken lowercase filename suffixes
         ///  </ Summary> 
        ///  <param name = "name"> filename </ param> 
        ///  <Returns> return lowercase suffixes, without "." </ Returns> 
        public  static  String getFileExt ( String name)
        {
            return name.Split(".").Last().ToLower();
        }

        ///  <the Summary> 
        /// whether the image file
         ///  </ the Summary> 
        ///  <param name = "FileExt"> file extension does not contain "." </ Param> 
        public  static  BOOL isImage ( String fileExt)
        {
            ArrayList al = new ArrayList { "bmp", "jpeg", "jpg", "gif", "png", "ico" };
            return al.Contains(fileExt);
        }

        ///  <Summary> 
        /// checks whether to allow file
         ///  </ Summary> 
        ///  <param name = "FileExt"> file suffix </ param> 
        ///  <param name = "allowExt"> Allow file array </ param> 
        public  static  BOOL CheckFileExt ( String FileExt, String [] allowExt)
        {
            return allowExt.Any(t => t == fileExt);
        }


        ///  <Summary> 
        /// thumbnail sized
         ///  </ Summary> 
        ///  <param name = "Original"> Image Object </ param> 
        ///  <param name = "newfilename"> New Path FIG. </ param> 
        ///  <param name = "maxWidth"> maximum width </ param> 
        ///  <param name = "the maxHeight"> maximum height </ param> 
        public  static  void ThumbImg (Original Image, String newfilename, int maxWidth, int for the 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> 
        /// thumbnail sized
         ///  </ Summary> 
        ///  <param name = "fileName"> filename </ param> 
        ///  <param name = "newfilename"> New Path FIG. </ param> 
        ///  <param name = "maxWidth"> maximum width </ param> 
        ///  <param name = "the maxHeight"> maximum height </ param> 
        public  static  void ThumbImg ( String fileName, String newfilename, int maxWidth, int for the maxHeight)
        {
            byte[] imageBytes = File.ReadAllBytes(fileName);
            Image img = Image.FromStream(new MemoryStream(imageBytes));
            ThumbImg(img, newFileName, maxWidth, maxHeight);
        }


        ///  <Summary> 
        /// calculate the new size
         ///  </ Summary> 
        ///  <param name = "width"> original width </ param> 
        ///  <param name = "height"> original height < / param> 
        ///  <param name = "maxWidth"> maximum width new </ param> 
        ///  <param name = "the maxHeight"> maximum height new </ param> 
        ///  <Returns> </ Returns> 
        Private  static Size ResizeImage ( int width, int height, int maxWidth, int for the 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);
        }


        ///  <the Summary> 
        /// get the picture format
         ///  </ the Summary> 
        ///  <param name = "name"> File name </ param> 
        ///  <returns A> </ returns A> 
        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;
            }
        }

 

An error

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:\src\WebApp\Common\VerifyCodeHelper.cs:line 206
   at WebApp.Controllers.VerifyCodeController.NumberVerifyCode() in F:\src\WebApp\Controllers\VerifyCodeController.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()

 

Solution

CentOS 7

yum install libgdiplus-devel

Guess you like

Origin www.cnblogs.com/chenyishi/p/12302058.html