From 3539a3cc27b8fcb32ae5107f381d9127e258eb8a Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Mon, 24 Feb 2020 13:04:19 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=9B=BE=E7=89=87=E5=89=AA?= =?UTF-8?q?=E8=A3=81=E7=BC=A9=E6=94=BE=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E5=88=B0base64=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 678167a4f9786a924881e153ff75d90f949cb1ee --- labs/CameraCard/CameraCard/CameraCard.csproj | 1 + .../CameraCard/Infrastructure/CVRHelper.cs | 1 + .../CameraCard/Infrastructure/ImageHelper.cs | 53 +++++++++++++++++++ labs/CameraCard/CameraCard/MainForm.cs | 36 +++---------- 4 files changed, 62 insertions(+), 29 deletions(-) create mode 100644 labs/CameraCard/CameraCard/Infrastructure/ImageHelper.cs diff --git a/labs/CameraCard/CameraCard/CameraCard.csproj b/labs/CameraCard/CameraCard/CameraCard.csproj index 088ba319..de8bf062 100644 --- a/labs/CameraCard/CameraCard/CameraCard.csproj +++ b/labs/CameraCard/CameraCard/CameraCard.csproj @@ -78,6 +78,7 @@ + diff --git a/labs/CameraCard/CameraCard/Infrastructure/CVRHelper.cs b/labs/CameraCard/CameraCard/Infrastructure/CVRHelper.cs index c747830b..12f26ce4 100644 --- a/labs/CameraCard/CameraCard/Infrastructure/CVRHelper.cs +++ b/labs/CameraCard/CameraCard/Infrastructure/CVRHelper.cs @@ -1,5 +1,6 @@ using System; using System.Drawing; +using System.Drawing.Imaging; using System.Globalization; using System.IO; using System.Text; diff --git a/labs/CameraCard/CameraCard/Infrastructure/ImageHelper.cs b/labs/CameraCard/CameraCard/Infrastructure/ImageHelper.cs new file mode 100644 index 00000000..498337d4 --- /dev/null +++ b/labs/CameraCard/CameraCard/Infrastructure/ImageHelper.cs @@ -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()); + } + } + } + } +} \ No newline at end of file diff --git a/labs/CameraCard/CameraCard/MainForm.cs b/labs/CameraCard/CameraCard/MainForm.cs index 6720adb8..93a20dff 100644 --- a/labs/CameraCard/CameraCard/MainForm.cs +++ b/labs/CameraCard/CameraCard/MainForm.cs @@ -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)