134 lines
4.7 KiB
C#
134 lines
4.7 KiB
C#
|
using iTextSharp.text.pdf;
|
|||
|
using iTextSharp.text;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Drawing;
|
|||
|
using System.Drawing.Imaging;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Net;
|
|||
|
|
|||
|
namespace 心电图绘制
|
|||
|
{
|
|||
|
internal class UniversalFun
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// base64转换成图片
|
|||
|
/// </summary>
|
|||
|
/// <param name="base64String"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static System.Drawing.Image ConvertFromBase64String(string base64String)
|
|||
|
{
|
|||
|
string [] baseary= base64String.Split(',');
|
|||
|
if(baseary.Length<2)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
// 将Base64字符串转换为字节数组
|
|||
|
byte[] imageBytes = Convert.FromBase64String(baseary[1]);
|
|||
|
|
|||
|
// 使用字节数组创建MemoryStream
|
|||
|
using (MemoryStream ms = new MemoryStream(imageBytes))
|
|||
|
{
|
|||
|
// 从MemoryStream中创建Image对象
|
|||
|
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
|
|||
|
return image;
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 图片转换成base64
|
|||
|
/// </summary>
|
|||
|
/// <param name="bitmap"></param>
|
|||
|
/// <param name="format"></param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string ConvertToBase64String(Bitmap bitmap, ImageFormat format)
|
|||
|
{
|
|||
|
// 将Bitmap保存到MemoryStream中
|
|||
|
using (MemoryStream ms = new MemoryStream())
|
|||
|
{
|
|||
|
bitmap.Save(ms, format); // 可以根据需要选择不同的图片格式,例如ImageFormat.Png或ImageFormat.Jpeg
|
|||
|
// 将流的位置重置到开始位置
|
|||
|
ms.Position = 0;
|
|||
|
// 将MemoryStream中的字节数组编码为Base64字符串
|
|||
|
byte[] imageBytes = ms.ToArray();
|
|||
|
string base64String = Convert.ToBase64String(imageBytes);
|
|||
|
return base64String;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 图片转换成PDF
|
|||
|
/// </summary>
|
|||
|
/// <param name="bitmap"></param>
|
|||
|
/// <param name="pdfPath"></param>
|
|||
|
public static void ConvertBitmapToPdf(Bitmap bitmap, string pdfPath)
|
|||
|
{
|
|||
|
|
|||
|
string directoryPath = Path.GetDirectoryName(pdfPath);
|
|||
|
if (directoryPath != null && !Directory.Exists(directoryPath))
|
|||
|
{
|
|||
|
Directory.CreateDirectory(directoryPath);
|
|||
|
}
|
|||
|
// 创建一个文档对象
|
|||
|
Document document = new Document();
|
|||
|
|
|||
|
// 创建一个PdfWriter实例
|
|||
|
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
|
|||
|
|
|||
|
// 打开文档以便添加内容
|
|||
|
document.Open();
|
|||
|
// 设置页面尺寸与图像尺寸一致
|
|||
|
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(bitmap.Width, bitmap.Height+50);
|
|||
|
document.SetPageSize(pageSize);
|
|||
|
// 将Bitmap转换为iTextSharp的Image对象
|
|||
|
iTextSharp.text.Image image = ConvertBitmapToImageSharp(bitmap);
|
|||
|
|
|||
|
// 将图像添加到文档
|
|||
|
document.NewPage();
|
|||
|
document.Add(image);
|
|||
|
|
|||
|
// 关闭文档
|
|||
|
document.Close();
|
|||
|
}
|
|||
|
|
|||
|
private static iTextSharp.text.Image ConvertBitmapToImageSharp(Bitmap bitmap)
|
|||
|
{
|
|||
|
using (MemoryStream ms = new MemoryStream())
|
|||
|
{
|
|||
|
// 将Bitmap保存到MemoryStream中,使用PNG格式
|
|||
|
bitmap.Save(ms, ImageFormat.Png);
|
|||
|
|
|||
|
// 从MemoryStream中创建iTextSharp的Image对象
|
|||
|
return iTextSharp.text.Image.GetInstance(new MemoryStream(ms.ToArray()));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取指定 URL 的文本文件内容
|
|||
|
/// </summary>
|
|||
|
/// <param name="url">文本文件的 URL</param>
|
|||
|
/// <returns>返回文件内容的字符串,如果失败则返回 null</returns>
|
|||
|
public static string GetTextFileContent(string url)
|
|||
|
{
|
|||
|
// 忽略 SSL 证书错误
|
|||
|
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
|
|||
|
using (WebClient client = new WebClient())
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
// 下载文件内容
|
|||
|
return client.DownloadString(url);
|
|||
|
}
|
|||
|
catch (WebException e)
|
|||
|
{
|
|||
|
Console.WriteLine("请求错误: " + e.Message);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|