add-添加照片压缩

master
liuwf 5 months ago
parent be7255bdd4
commit 090fc9ea0b

@ -179,7 +179,9 @@ namespace SlnMesnac.Business
ImageData = FileHelper.FindPhoto(config.CameraFilePath); ImageData = FileHelper.FindPhoto(config.CameraFilePath);
if (ImageData != null) if (ImageData != null)
{ {
FileHelper.SaveImage(ImageData, materialCodeStr + ".jpg", PicturePath); // 图片压缩
byte[] compressedImageData = FileHelper.CompressImageData(ImageData, 20);
FileHelper.SaveImage(compressedImageData, materialCodeStr + ".jpg", PicturePath);
RefreshPictureEvent?.Invoke(ImageData); RefreshPictureEvent?.Invoke(ImageData);
} }
else else

@ -1,13 +1,66 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Encoder = System.Drawing.Imaging.Encoder;
namespace SlnMesnac.Common namespace SlnMesnac.Common
{ {
public static class FileHelper public static class FileHelper
{ {
#region
/// <summary>
///quality: 0-100 ,越小压缩程度也越高,但是图片质量越低
/// </summary>
/// <param name="imageData"></param>
/// <param name="quality"></param>
/// <returns></returns>
public static byte[] CompressImageData(byte[] imageData, long quality)
{
try
{
using (MemoryStream memoryStream = new MemoryStream(imageData))
{
using (Image image = Image.FromStream(memoryStream))
{
using (MemoryStream compressedStream = new MemoryStream())
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
Encoder myEncoder = Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
myEncoderParameters.Param[0] = myEncoderParameter;
image.Save(compressedStream, jpgEncoder, myEncoderParameters);
return compressedStream.ToArray();
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"压缩图片出错:{ex.Message}");
return null;
}
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
#endregion
/// <summary> /// <summary>
/// 查找指定目录下的文件 /// 查找指定目录下的文件

@ -15,6 +15,14 @@ namespace SlnMesnac.Common
#region 单例实现 #region 单例实现
private static readonly GunHelper lazy = new GunHelper(); private static readonly GunHelper lazy = new GunHelper();
public static GunHelper Instance
{
get
{
return lazy;
}
}
#endregion
#region 变量定义 #region 变量定义
private static SerialPort serialPort = new SerialPort(); private static SerialPort serialPort = new SerialPort();
@ -29,14 +37,7 @@ namespace SlnMesnac.Common
public delegate void RefreshMaterialCodeStr(string code); public delegate void RefreshMaterialCodeStr(string code);
public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent; public static event RefreshMaterialCodeStr RefreshMaterialCodeStrEvent;
public static GunHelper Instance
{
get
{
return lazy;
}
}
#endregion
private DebugConfig config = DebugConfig.Instance; private DebugConfig config = DebugConfig.Instance;

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
namespace SlnMesnac.Common
{
public class ImageHelper
{
#region 单例实现
private static readonly ImageHelper lazy = new ImageHelper();
public static ImageHelper Instance
{
get
{
return lazy;
}
}
#endregion
public void CompressImage(string sourceImagePath, string destinationPath, long quality)
{
using (Image image = Image.FromFile(sourceImagePath))
{
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
myEncoderParameters.Param[0] = myEncoderParameter;
image.Save(destinationPath, jpgEncoder, myEncoderParameters);
}
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
}
}

@ -75,9 +75,7 @@ namespace SlnMesnac.WPF.ViewModel
/// <exception cref="NotImplementedException"></exception> /// <exception cref="NotImplementedException"></exception>
private void Reset() private void Reset()
{ {
logoBusiness.Pass();
logoBusiness.Pass();
} }
/// <summary> /// <summary>

Loading…
Cancel
Save