string path = "D:\\img\\xiedl\\2020-11-20\\1 (" + (z - 43210) + ").png";
string base64Str = ReadFromFile(path);
Bitmap bitmap = Base64StringToImage(base64Str);
Bitmap bitmap2 = ZoomImage(bitmap, 300, 300);
base64Str = ToBase64(bitmap2);
private static string ReadFromFile(string path)
{
FileStream fsForRead = new FileStream(path, FileMode.Open);
string base64Str = "";
try
{
//读入一个字节
Console.Write("文件的第一个字节为:" + fsForRead.ReadByte().ToString());
Console.ReadLine();
//读写指针移到距开头10个字节处
fsForRead.Seek(0, SeekOrigin.Begin);
byte[] bs = new byte[fsForRead.Length];
int log = Convert.ToInt32(fsForRead.Length);
//从文件中读取10个字节放到数组bs中
fsForRead.Read(bs, 0, log);
base64Str = Convert.ToBase64String(bs);
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadLine();
}
finally
{
fsForRead.Close();
}
return base64Str;
}
//base64编码的文本转为图片
private Bitmap Base64StringToImage(string inputStr)
{
try
{
byte[] arr = Convert.FromBase64String(inputStr);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
ms.Close();
return bmp;
}
catch (Exception ex)
{
MessageBox.Show("Base64StringToImage 转换失败/nException:" + ex.Message);
return null;
}
}
//图片转为base64编码的文本
private string ToBase64(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();
String strbaser64 = Convert.ToBase64String(arr);
return strbaser64;
}
catch (Exception ex)
{
MessageBox.Show("ImgToBase64String 转换失败 Exception:" + ex.Message);
return "";
}
}
private Bitmap ZoomImage(Bitmap bitmap, int destHeight, int destWidth)
{
try
{
System.Drawing.Image sourImage = bitmap;
int width = 0, height = 0;
//按比例缩放
int sourWidth = sourImage.Width;
int sourHeight = sourImage.Height;
if (sourHeight > destHeight || sourWidth > destWidth)
{
if ((sourWidth * destHeight) > (sourHeight * destWidth))
{
width = destWidth;
height = (destWidth * sourHeight) / sourWidth;
}
else
{
height = destHeight;
width = (sourWidth * destHeight) / sourHeight;
}
}
else
{
width = sourWidth;
height = sourHeight;
}
Bitmap destBitmap = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage(destBitmap);
g.Clear(System.Drawing.Color.White);
//设置画布的描绘质量
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(sourImage, new System.Drawing.Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
g.Dispose();
//设置压缩质量
System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
long[] quality = new long[1];
quality[0] = 100;
System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
encoderParams.Param[0] = encoderParam;
sourImage.Dispose();
return destBitmap;
}
catch
{
return bitmap;
}
}