299 lines
11 KiB
C#
299 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Work.BLL.Tools
|
|
{
|
|
public class Base64ChangeImgUrl
|
|
{
|
|
|
|
/// <summary>
|
|
/// 将Base64字符串转换为图片
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
public Bitmap ToImage(string imgBase64)
|
|
{
|
|
imgBase64 = imgBase64.Substring("data:image/jpeg;base64,".Length);
|
|
byte[] bytes = Convert.FromBase64String(imgBase64);
|
|
MemoryStream ms = new MemoryStream(bytes);
|
|
Image mImage = Image.FromStream(ms);
|
|
Bitmap bmpt = new Bitmap(ms);
|
|
return bmpt;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 图片转Base64
|
|
/// </summary>
|
|
/// <param name="ImageFileName">图片的完整路径</param>
|
|
/// <returns></returns>
|
|
public static string ImgToBase64(Bitmap bmp)
|
|
{
|
|
try
|
|
{
|
|
MemoryStream ms = new MemoryStream();
|
|
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
byte[] arr = new byte[ms.Length];
|
|
ms.Position = 0;
|
|
ms.Read(arr, 0, (int)ms.Length);
|
|
ms.Close();
|
|
return Convert.ToBase64String(arr);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public string CutZoom(string Base64pic)
|
|
{
|
|
|
|
byte[] arr2 = Convert.FromBase64String(Base64pic);
|
|
Bitmap bmp3 = null;
|
|
using (MemoryStream ms2 = new MemoryStream(arr2))
|
|
{
|
|
Bitmap bmp2 = new System.Drawing.Bitmap(ms2);
|
|
bmp3 = GetThumbnail(bmp2, 130, 270);
|
|
}
|
|
|
|
return ImgToBase64(bmp3);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 图片转为二进制
|
|
/// </summary>
|
|
/// <param name="Bitmap"></param>
|
|
/// <returns></returns>
|
|
public byte[] GetPictureData(Bitmap Bitmap)
|
|
{
|
|
MemoryStream ms = null;
|
|
try
|
|
{
|
|
ms = new MemoryStream();
|
|
Bitmap.Save(ms, Bitmap.RawFormat);
|
|
byte[] byteImage = new Byte[ms.Length];
|
|
byteImage = ms.ToArray();
|
|
return byteImage;
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
ms.Close();
|
|
}
|
|
}
|
|
|
|
public byte[] Base64ChangeBuffer(String imgBase64)
|
|
{
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(imgBase64))
|
|
{
|
|
return GetPictureData(ToImage(imgBase64));
|
|
}
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static Bitmap GetThumbnail(Bitmap b, int destHeight, int destWidth)
|
|
{
|
|
System.Drawing.Image imgSource = b;
|
|
System.Drawing.Imaging.ImageFormat thisFormat = imgSource.RawFormat;
|
|
int sW = 0, sH = 0;
|
|
// 按比例缩放
|
|
int sWidth = imgSource.Width;
|
|
int sHeight = imgSource.Height;
|
|
if (sHeight > destHeight || sWidth > destWidth)
|
|
{
|
|
if ((sWidth * destHeight) > (sHeight * destWidth))
|
|
{
|
|
sW = destWidth;
|
|
sH = (destWidth * sHeight) / sWidth;
|
|
}
|
|
else
|
|
{
|
|
sH = destHeight;
|
|
sW = (sWidth * destHeight) / sHeight;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sW = sWidth;
|
|
sH = sHeight;
|
|
}
|
|
Bitmap outBmp = new Bitmap(destWidth, destHeight);
|
|
Graphics g = Graphics.FromImage(outBmp);
|
|
g.Clear(Color.Transparent);
|
|
// 设置画布的描绘质量
|
|
g.CompositingQuality = CompositingQuality.HighQuality;
|
|
g.SmoothingMode = SmoothingMode.HighQuality;
|
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
g.DrawImage(imgSource, new Rectangle((destWidth - sW) / 2, (destHeight - sH) / 2, sW, sH), 0, 0, imgSource.Width, imgSource.Height, GraphicsUnit.Pixel);
|
|
g.Dispose();
|
|
// 以下代码为保存图片时,设置压缩质量
|
|
EncoderParameters encoderParams = new EncoderParameters();
|
|
long[] quality = new long[1];
|
|
quality[0] = 100;
|
|
EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
|
|
encoderParams.Param[0] = encoderParam;
|
|
imgSource.Dispose();
|
|
return outBmp;
|
|
}
|
|
|
|
public int CalculateNick(Bitmap bitmap, int v)
|
|
{
|
|
int iNickX = 0;
|
|
int iDis = 5; //差异值
|
|
List<int> lstiNickX = new List<int>();
|
|
for (int y = 0; y < bitmap.Height; y++)
|
|
{
|
|
for (int x = 0; x < bitmap.Width - v; x++)
|
|
{
|
|
Color color1 = bitmap.GetPixel(x, y);
|
|
Color color2 = bitmap.GetPixel(x + 1, y);
|
|
int icolor1 = GetRGBFromColor(color1);
|
|
int icolor2 = GetRGBFromColor(color2);
|
|
double dDistance = GetDistance(color2, color1);
|
|
if (dDistance > iDis && icolor2 < icolor1)
|
|
{
|
|
for (int i = 0; i < v + v / 2; i++)
|
|
{
|
|
iNickX = x + i;
|
|
if (iNickX >= bitmap.Width - 1)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Color colorNickPre = bitmap.GetPixel(iNickX, y);
|
|
Color colorNickAft = bitmap.GetPixel(iNickX + 1, y);
|
|
|
|
int icolorNickPre = GetRGBFromColor(colorNickPre);
|
|
int icolorNickAft = GetRGBFromColor(colorNickAft);
|
|
double dNickDistance = GetDistance(colorNickAft, colorNickPre);
|
|
if (dNickDistance > dDistance && icolorNickAft > icolorNickPre)
|
|
{
|
|
int iGap = Math.Abs(iNickX - x - v);
|
|
if (iGap <= 2)
|
|
{
|
|
lstiNickX.Add(x);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (lstiNickX.Count > 0)
|
|
{
|
|
iNickX = GetRight(lstiNickX);
|
|
//label1.Text = iNickX.ToString();
|
|
}
|
|
return iNickX;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 滑块的宽度
|
|
/// </summary>
|
|
/// <param name="bitmap"></param>
|
|
/// <param name="v"></param>
|
|
/// <returns></returns>
|
|
public int CalculateNickNew(Bitmap bitmap, int v = 39)
|
|
{
|
|
int iNickX = 0;
|
|
decimal idif = 0.50m; // 差异比例
|
|
List<int> lstiNickX = new List<int>();
|
|
for (int y = 0; y < bitmap.Height; y++)
|
|
{
|
|
for (int x = 0; x < bitmap.Width - v - 1; x++)
|
|
{
|
|
Color color1 = bitmap.GetPixel(x, y);
|
|
Color color2 = bitmap.GetPixel(x + 1, y);
|
|
int icolor1 = color1.R + color1.G + color1.B;
|
|
int icolor2 = color2.R + color2.G + color2.B;
|
|
if (color2.R < color1.R * (1 - idif) && color2.G < color1.G * (1 - idif) && color2.G < color1.G * (1 - idif))
|
|
{
|
|
for (int i = 1; i < v + v / 2; i++)
|
|
{
|
|
iNickX = x + i;
|
|
if (iNickX >= bitmap.Width - 1)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Color color3 = bitmap.GetPixel(iNickX, y);
|
|
Color color4 = bitmap.GetPixel(iNickX + 1, y);
|
|
|
|
int icolor3 = GetRGBFromColor(color3);
|
|
int icolor4 = GetRGBFromColor(color4);
|
|
if (color3.R < color4.R * (1 - idif) && color3.G < color4.G * (1 - idif) && color3.G < color4.G * (1 - idif))
|
|
{
|
|
int iGap = Math.Abs(iNickX - x - v);
|
|
if (iGap <= 2)
|
|
{
|
|
lstiNickX.Add(x);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (lstiNickX.Count > 0)
|
|
{
|
|
iNickX = GetRight(lstiNickX);
|
|
//label1.Text = iNickX.ToString();
|
|
}
|
|
return iNickX + 1;
|
|
}
|
|
|
|
public int GetRGBFromColor(Color color)
|
|
{
|
|
return (int)(((int)color.B << 16) | (ushort)(((ushort)color.G << 8) | color.R));
|
|
byte r = color.R;
|
|
byte g = color.G;
|
|
byte b = color.B;
|
|
//转化为32bit RGB值:
|
|
int rgb = r + g * 256 + b * 256 * 256;
|
|
return rgb;
|
|
}
|
|
|
|
private double GetDistance(Color color1, Color color2)
|
|
{
|
|
double y1, u1, v1, y2, u2, v2;
|
|
y1 = 0.299 * color1.R + 0.587 * color1.G + 0.114 * color1.B;
|
|
u1 = -0.14713 * color1.R - 0.28886 * color1.G + 0.436 * color1.B;
|
|
v1 = 0.615 * color1.R - 0.51498 * color1.G - 0.10001 * color1.B;
|
|
y2 = 0.299 * color2.R + 0.587 * color2.G + 0.114 * color2.B;
|
|
u2 = -0.14713 * color2.R - 0.28886 * color2.G + 0.436 * color2.B;
|
|
v2 = 0.615 * color2.R - 0.51498 * color2.G - 0.10001 * color2.B;
|
|
return Math.Sqrt((y1 - y2) * (y1 - y2) + (u1 - u2) * (u1 - u2) + (v1 - v2) * (v1 - v2));
|
|
}
|
|
|
|
private int GetRight(List<int> lstiNickX)
|
|
{
|
|
var res = from n in lstiNickX
|
|
group n by n into g
|
|
orderby g.Count() descending
|
|
select g;
|
|
var gr = res.First();
|
|
return gr.Key;
|
|
}
|
|
}
|
|
}
|