重构图片剪裁缩放,添加转换到base64扩展方法

Former-commit-id: 678167a4f9786a924881e153ff75d90f949cb1ee
TangShanKaiPing
wanggang 5 years ago
parent 6ef2329321
commit 3539a3cc27

@ -78,6 +78,7 @@
<ItemGroup>
<Compile Include="CVRSDK.cs" />
<Compile Include="Infrastructure\AForgeCameraHelper.cs" />
<Compile Include="Infrastructure\ImageHelper.cs" />
<Compile Include="Infrastructure\CVRHelper.cs" />
<Compile Include="Infrastructure\CVRSDK.cs" />
<Compile Include="Infrastructure\ICameraHelper.cs" />

@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Text;

@ -0,0 +1,53 @@
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
public static class ImageHelper
{
public static Bitmap ClipImage(this Image image, int renderWidth, int renderHeight, int rectWidth, int rectHeight)
{
var width = rectWidth * image.Width / renderWidth;
var height = rectHeight * width / rectWidth;
var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bitmap.SetResolution(350, 350);
using (var g = Graphics.FromImage(bitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle((image.Width - bitmap.Width) / 2, (image.Height - bitmap.Height) / 2, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
}
return bitmap;
}
public static Bitmap ResizeImage(this Image image, int width, int height)
{
var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bitmap.SetResolution(350, 350);
using (var g = Graphics.FromImage(bitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
}
return bitmap;
}
public static string ToBase64(this Image image)
{
var info = ImageCodecInfo.GetImageEncoders().First(o => o.FormatID == ImageFormat.Jpeg.Guid);
using (var encoderParams = new EncoderParameters(1))
{
using (var param = new EncoderParameter(Encoder.Quality, 70L))
{
encoderParams.Param[0] = param;
//image.Save("temp.jpg", info, encoderParams);
using (var ms = new MemoryStream())
{
image.Save(ms, info, encoderParams);
return Convert.ToBase64String(ms.ToArray());
}
}
}
}
}

@ -108,37 +108,15 @@ namespace CameraCard
private void shotBtn_Click(object sender, EventArgs e)
{
var bitmap = this.ClipImage();
bitmap = this.ResizeImage(bitmap);
this.photo.Image?.Dispose();
this.photo.Image = bitmap;
}
private Bitmap ClipImage()
{
var image = this.cameraRender.Image;
var width = this._rect.Width * image.Width / this.cameraRender.Width;
var height = this._rect.Height * width / this._rect.Width;
var bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
bitmap.SetResolution(350, 350);
using (var g = Graphics.FromImage(bitmap))
using (var bitmap = (this.cameraRender.Image.Clone() as Image))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle((image.Width - bitmap.Width) / 2, (image.Height - bitmap.Height) / 2, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
}
return bitmap;
}
private Bitmap ResizeImage(Bitmap image)
{
var bitmap = new Bitmap(this._photoWidth, this._photoHeight, PixelFormat.Format24bppRgb);
bitmap.SetResolution(350, 350);
using (var g = Graphics.FromImage(bitmap))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
using (var bitmap2 = bitmap.ClipImage(this.cameraRender.Width, this.cameraRender.Height, this._rectWidth, this._rectHeight))
{
this.photo.Image?.Dispose();
this.photo.Image = bitmap2.ResizeImage(this._photoWidth, this._photoHeight);
var base64Value = $"data:image/jpeg;base64,{this.photo.Image.ToBase64()}";
}
}
return bitmap;
}
private void UpdateIdCardUI(IdCardModel model)

Loading…
Cancel
Save