首先,隆重介绍今天的主角:ImageFactory(http://imageprocessor.org/)。虽然我并没有在实际工作中用到这个库,但是它干净利索的使用方式打动了我,很久以前就存了下来。这个库的开发语言是C#,nuget有下载,提供了一系列可以链式调用的方法来处理图片。关键的一点,它是开源的,代码很清晰,有什么bug和不爽可以自己想怎么改就怎么改。
接下来,需要一提的是,我对图片处理一窍不通。所以,这篇文章是我入门图片处理的记录。当然,矩形圆形直线、RGB这类基础,就不特别提了。
一、图片格式
ImageFactory内置支持的图片格式,自然也是常见常用的图片格式。包括:
1、bmp:Bitmap,位图,采用位映射存储格式,除了图像深度可选以外,不采用其他任何压缩。
2、gif:Graphics Interchange Format,分为静态GIF和动画GIF,是一种压缩位图格式。
3、jpg(jpeg):Joint Photographic Experts GROUP,可以选择压缩级别。
4、png:Portable Network Graphic Format,其设计目的是试图替代GIF和TIFF文件格式,同时增加一些GIF文件格式所不具备的特性。
5、tif(tiff):Tag Image File Format,是一种灵活的位图格式,主要用来存储包括照片和艺术图在内的图像。
二、图片处理
Alpha 透明度
Changes the opacity of the current image.
Alpha(int percentage) [0,100]
AutoRotate 自转
Performs auto-rotation to ensure that EXIF defined rotation is reflected in the final image.
AutoRotate()
BackgroundColor 背景色
Changes the background color of the current image.
BackgroundColor(Color color)
Brightness 亮度
Changes the brightness of the current image.
Brightness(int percentage) [-100,100]
Constrain 约束
Constrains the current image, resizing it to fit within the given dimensions whilst keeping its aspect ratio. Produces the same output as the Resize method using ResizeMode.Max
Constrain(Size size)
Contrast 对比度
Changes the contrast of the current image.
Contrast(int percentage) [-100,100]
Crop 裁剪
Crops the current image to the given location and size.
Crop(Rectangle rectangle)
Crop(CropLayer cropLayer)
DetectEdges 边缘检测
Detects the edges in the current image using various one and two dimensional algorithms. If the greyscale parameter is set to false the detected edges will maintain the pixel colors of the originl image.
DetectEdges(IEdgeFilter filter, bool greyscale = true)
IEdgeFilter = KayyaliEdgeFilter、KirschEdgeFilter、Laplacian3X3EdgeFilter、PrewittEdgeFilter...
EntropyCrop 熵裁剪(根据信息量的阈值来裁剪)
Crops an image to the area of greatest entropy. This method works best with images containing large areas of a single color or similar colors around the edges.
EntropyCrop(byte threshold = 128)
Filter 滤镜
Applies a filter to the current image. Use the MatrixFilters class to assign the correct filter
Filter(IMatrixFilter matrixFilter)
IMatrixFilter = BlackWhite、Gotham、Lomograph、Polaroid...
Flip 翻转
Flips the current image either horizontally or vertically.
Flip(bool flipVertically)
Format 格式
Sets the output format of the current image to the matching
Format(ISupportedImageFormat format)
ISupportedImageFormat = BitmapFormat、JpegFormat、GifFormat...
GaussianBlur 高斯模糊
Uses a Gaussian kernel to blur the current image.
GaussianBlur(int size)
GaussianBlur(GaussianLayer gaussianLayer)
GaussianSharpen 高斯锐化
Uses a Gaussian kernel to sharpen the current image.
GaussianSharpen(int size)
GaussianSharpen(GaussianLayer gaussianLayer)
Hue 色调
Alters the hue of the current image changing the overall color.
Hue(int degrees, bool rotate = false)
Mask 遮盖
Applies the given image mask to the current image. Any area containing transparency withing the mask will be removed from the original image. If the mask is larger than the image it will be resized to match the images dimensions.
Mask(Image imageMask, Point? point = null)
Overlay 覆盖
Adds a image overlay to the current image. If the overlay is larger than the image it will be resized to match the images dimensions.
Overlay(ImageLayer imageLayer)
Pixelate 像素化(马赛克)
Pixelates an image with the given size.
Pixelate(int pixelSize, Rectangle? rectangle = null)
Quality 图片质量
Alters the output quality of the current image. This method will only effect the output quality of jpeg images.
Quality(int percentage) [0,100]
ReplaceColor 替换颜色
Replaces a color within the current image.
ReplaceColor(Color target, Color replacement, int fuzziness = 0)
Resize 改变大小
Resizes the current image to the given dimensions. If EXIF metadata is to be preserved the information contained within will also be updated to match.
Resize(Size size)
Resize(ResizeLayer resizeLayer)
Resolution 分辨率
Changes the horizontal and vertical resolution of the image. If EXIF metadata is to be preserved the information contained within will also be updated to match.
Resolution(int w, int h)
Rotate 旋转
Rotates the current image by the given angle without clipping.
Rotate(int degrees)
RoundedCorners 圆整圆角
Adds rounded corners to the current image.
RoundedCorners(int radius)
RoundedCorners(RoundedCornerLayer roundedCornersLayer)
Saturation 饱和度
Changes the saturation of the current image.
Saturation(int percentage)
Tint 色度
Tints the current image with the given color
Tint(Color color)
Vignette 晕影
Adds a vignette image effect to the current image.
Vignette(Color color)
Watermark 水印
Adds a text based watermark to the current image.
Watermark (TextLayer textLayer)
三、尝试
最后,来试一下效果吧。