📄 imagehelper.cs
字号:
namespace Imps.Utils
{
using Imps.Client.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public class ImageHelper
{
public static Image Base64ToImage(string strInput)
{
try
{
using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(strInput)))
{
return Image.FromStream(stream);
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
return null;
}
public static Image Base64ToImage(string strInput, int width, int height)
{
Image image = Base64ToImage(strInput);
if (image != null)
{
return GetThumbnailImage(image, width, height);
}
return image;
}
public static void BatchTransform(TransformTaskCollection taksColl)
{
using (IEnumerator<TransformTask> enumerator = taksColl.GetEnumerator())
{
while (enumerator.MoveNext())
{
TransformTask task = enumerator.get_Current();
Transform(task.Image, task.tf);
}
}
}
public static Bitmap ConvertToGrayscale(Image source)
{
try
{
Bitmap srcImg = new Bitmap(source);
return new Grayscale(0.2125f, 0.7154f, 0.0721f).Apply(srcImg);
}
catch
{
return null;
}
}
[Obsolete("请使用TryGetTransparentImage替代", false)]
public static Bitmap GetAlphaImage(Image img, float percent)
{
return new Bitmap(TryGetTransparentImage(img, percent));
}
public static void GetHighQualifiedJpegStream(MemoryStream ms, Image input, int size)
{
GetHighQualifiedJpegStream(ms, input, size, Color.White);
}
public static void GetHighQualifiedJpegStream(MemoryStream ms, string path, int size)
{
GetHighQualifiedJpegStream(ms, path, size, Color.White);
}
public static void GetHighQualifiedJpegStream(MemoryStream ms, Image input, int size, Color bgColor)
{
if (ms == null)
{
throw new ArgumentNullException("ms");
}
if (input == null)
{
throw new ArgumentNullException("input");
}
using (Image image = GetSquareThumbnailImage(input, size))
{
using (Bitmap bitmap = new Bitmap(size, size))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.FillRectangle(new SolidBrush(bgColor), 0, 0, size, size);
graphics.DrawImage(image, 0, 0);
bitmap.Save(ms, GetImageCodecInfo(ImageFormat.Jpeg), JpegParms());
}
}
}
}
public static void GetHighQualifiedJpegStream(MemoryStream ms, string path, int size, Color bgColor)
{
GetHighQualifiedJpegStream(ms, Image.FromFile(path), size, bgColor);
}
public static ImageCodecInfo GetImageCodecInfo(ImageFormat format)
{
foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
{
if (info.FormatID == format.Guid)
{
return info;
}
}
return null;
}
public static int GetImageFrameCount(Image image)
{
try
{
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
return image.GetFrameCount(dimension);
}
catch
{
return 1;
}
}
public static Image GetScaleGifImage(Image image, int width, int height, bool keepRatio)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
return null;
}
public static Image GetScaleGifImage(string path, int width, int height, bool keepRatio)
{
if (!File.Exists(path))
{
throw new ArgumentNullException(path);
}
try
{
return GetScaleGifImage(Image.FromFile(path), width, height, keepRatio);
}
catch
{
return null;
}
}
public static Image GetSlicedImage(Image image, int x, int y, int width, int height)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
ImpSliceTransform tf = new ImpSliceTransform(x, y, width, height);
return Transform(image, tf);
}
public static Image GetSlicedImage(string path, int x, int y, int width, int height)
{
if (!File.Exists(path))
{
throw new FileNotFoundException(path);
}
try
{
return GetSlicedImage(Image.FromFile(path), x, y, width, height);
}
catch
{
return null;
}
}
public static Image GetSquareThumbnailImage(Image image, int size)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
ImpSquareScaleTransform tf = new ImpSquareScaleTransform(size);
return Transform(image, tf);
}
public static Image GetSquareThumbnailImage(string path, int size)
{
if (!File.Exists(path))
{
throw new FileNotFoundException(path);
}
try
{
return GetSquareThumbnailImage(Image.FromFile(path), size);
}
catch
{
return null;
}
}
public static Image GetThumbnailImage(Image image, int width, int height)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
return GetThumbnailImage(image, width, height, false);
}
public static Image GetThumbnailImage(string path, int width, int height)
{
if (!File.Exists(path))
{
throw new FileNotFoundException(path);
}
try
{
return GetThumbnailImage(Image.FromFile(path), width, height);
}
catch
{
return null;
}
}
public static Image GetThumbnailImage(Image image, int width, int height, bool keepRatio)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
ImpScaleTransform tf = new ImpScaleTransform(width, height, keepRatio);
return Transform(image, tf);
}
public static Image GetTransparentImage(Image image, float percentage)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
ImpAlphaBlendingTransform tf = new ImpAlphaBlendingTransform(percentage);
return Transform(image, tf);
}
public static Image GetTransparentImage(string path, float percentage)
{
if (!File.Exists(path))
{
throw new FileNotFoundException(path);
}
try
{
return GetTransparentImage(Image.FromFile(path), percentage);
}
catch
{
return null;
}
}
public static string Image2Base64(Image image)
{
string text;
try
{
using (MemoryStream stream = new MemoryStream())
{
if (GetImageCodecInfo(image.RawFormat) != null)
{
image.Save(stream, image.RawFormat);
}
else
{
image.Save(stream, GetImageCodecInfo(ImageFormat.Jpeg), JpegParms());
}
text = Convert.ToBase64String(stream.GetBuffer());
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
text = string.Empty;
}
return text;
}
public static string Image2Base64(Image image, int size)
{
return Image2Base64(image, size, size);
}
public static string Image2Base64(Image image, int width, int height)
{
string text = string.Empty;
try
{
using (Image image2 = GetThumbnailImage(image, width, height, true))
{
using (Bitmap bitmap = new Bitmap(width, height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
graphics.DrawImage(image2, 0, 0);
return Image2Base64(bitmap);
}
}
}
}
catch (Exception exception)
{
ClientLogger.WriteException(exception);
}
return text;
}
public static Image Image2Jpg(Image input)
{
return Image2Jpg(input, input.Width);
}
public static Image Image2Jpg(Image input, int size)
{
return GetSquareThumbnailImage(input, size);
}
public static MemoryStream Image2JpgStream(Image input)
{
return Image2JpgStream(input, input.Width);
}
public static MemoryStream Image2JpgStream(Image input, int size)
{
MemoryStream stream = null;
stream = new MemoryStream();
Image2Jpg(input, size).Save(stream, ImageFormat.Jpeg);
return stream;
}
public static Image IsImageFile(string path)
{
try
{
return Image.FromFile(path);
}
catch
{
}
return null;
}
public static EncoderParameters JpegParms()
{
GetImageCodecInfo(ImageFormat.Jpeg);
EncoderParameters parameters = new EncoderParameters(1);
EncoderParameter parameter = new EncoderParameter(Encoder.Quality, (long) 0x5f);
parameters.Param[0] = parameter;
return parameters;
}
public static Image RotateImage(Image image, RotateFlipType rotateFlipType)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
try
{
image.RotateFlip(rotateFlipType);
return image;
}
catch
{
return null;
}
}
public static Image RotateImage(Image image, float angle)
{
if (image == null)
{
throw new ArgumentNullException("image");
}
return null;
}
public static Image RotateImage(string path, float angle)
{
if (!File.Exists(path))
{
throw new FileNotFoundException(path);
}
try
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -