C# 图片生成高清文件设置

private void workService()

{

try

{

foreach (Control control in this.Controls) control.Enabled = false;

progressBar.Enabled = true;

this.Cursor = Cursors.WaitCursor;

string url = txtApiUrl.Text.Trim();

string imagesPath = txtImagesPath.Text.Trim();

string[] imageFiles = Directory.GetFiles(imagesPath, "*.jpg", SearchOption.AllDirectories);

if (imageFiles.Length == 0)

{

MessageBox.Show("图片目录中没有找到JPG图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

return;

}

int fileIndex = 1;

Random random = new Random();

progressBar.Value = 0;

progressBar.Minimum = 0;

progressBar.Maximum = txtContents.Lines.Length;

foreach (string line in txtContents.Lines)

{

progressBar.Value += 1;

string contents = line.Trim();

if (contents == "") continue;

//cliD 是 string 二维码动态内容

string cliT = txtQRSTyle.Text.Trim();

string cliD = HttpUtility.UrlEncode(contents);

string key = txtApiKey.Text.Trim();

string parameters = $"api_key={key}&cliD={cliD}&cliT={cliT}&return_file=base64";

string base64 = HttpHelper.GetHtmlSource(url + "?" + parameters);

// 将Base64字符串转换为字节数组

base64 = base64.Replace("data:image/png;base64,", "");

byte[] imageBytes = Convert.FromBase64String(base64);

string qr_fn = Path.Combine(Application.StartupPath, "qr.png");

// 使用MemoryStream将字节数组转换为Image对象

using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))

{

ms.Write(imageBytes, 0, imageBytes.Length);

Image image = Image.FromStream(ms, true);

image.Save(qr_fn, System.Drawing.Imaging.ImageFormat.Png);

}

string girlImageFn = imageFiles[random.Next(0, imageFiles.Length)];

string backgroud_imgfn = Path.Combine(Application.StartupPath, "Backgroud.png");

string makeFriends_imgfn = Path.Combine(Application.StartupPath, "MakeFriends.png");

using (Bitmap backgroundImage = (Bitmap)Image.FromFile(backgroud_imgfn))

{

// ZoomPicture(girl_fn, backgroundImage.Width - 283, backgroundImage.Height);

// 加载要画上去的小图

using (Image qrImg = Image.FromFile(qr_fn))

{

using (Image makeFrendsImg = Image.FromFile(makeFriends_imgfn))

{

using (Image girlImg = Image.FromFile(girlImageFn))

{

// 在背景图上绘制小图

using (Graphics graphics = Graphics.FromImage(backgroundImage))

{

// 设置高质量插值法以保持图像质量

graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;

graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

graphics.DrawImage(qrImg,

new RectangleF(0, 285, 289, 285),

new RectangleF(0, 0, qrImg.Width, qrImg.Height),

GraphicsUnit.Pixel);

graphics.DrawImage(girlImg,

new RectangleF(283, 0, backgroundImage.Width - 283, backgroundImage.Height)

);

graphics.DrawImage(makeFrendsImg,

new PointF(

288 + ((288 - makeFrendsImg.Width) / 2),

285.5f + (285 - makeFrendsImg.Height) / 2));

}

}

}

string saveFn = Path.Combine(txtSavePath.Text.Trim(), fileIndex++ + ".Png");

ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Png);

System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;

// 保存合成后的图片为JPG格式,并设置高质量的压缩级别

// 创建一个EncoderParameters对象.

// 一个EncoderParameters对象有一个EncoderParameter数组对象

EncoderParameters encoderParams = new EncoderParameters(1);

EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);//设置为100时,比原图大了几KB。

encoderParams.Param[0] = myEncoderParameter;

backgroundImage.Save(saveFn, jgpEncoder, encoderParams);

}

}

}

MessageBox.Show("操作完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

catch (ThreadAbortException) { }

catch (Exception ex)

{

MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

}

finally

{

foreach (Control control in this.Controls) control.Enabled = true;

this.Cursor = Cursors.Default;

}

}

private ImageCodecInfo GetEncoder(ImageFormat format)

{

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();

foreach (ImageCodecInfo codec in codecs)

{

if (codec.FormatID == format.Guid)

{

return codec;

}

}

return null;

}

批量修改文件名精灵 2024年最新版
批量修改文件名称、文件夹名称

免费使用 功能无限制

方便快捷,支持正则表达式、无需安装功能齐全、支持定制无广告、无插件,放心使用。

免费下载 百度网盘
本站中所有的计算器的计算结果仅供参考,本站对此结果的准确性不承担任何责任,实际数额以银行/保险公司/国家相关机构确认的结果为准。
在线客服QQ:543690914,备案号: 苏ICP备15037649号-33。东海县白塔埠镇佳诚电脑经营部版权所有。