物流公司创建,长春seo网站管理,电子商务公司管理制度,外贸企业做网站Base64 编码是一种二进制到文本的编码方案#xff0c;可有效地将二进制数据转换为 ASCII 字符#xff0c;为数据交换提供通用格式。在某些情况下#xff0c;我们可能需要将JPG或PNG图像转换为 Base64 字符串数据。在这篇博文中#xff0c;我们将学习如何在 C# 中将图像转换…Base64 编码是一种二进制到文本的编码方案可有效地将二进制数据转换为 ASCII 字符为数据交换提供通用格式。在某些情况下我们可能需要将JPG或PNG图像转换为 Base64 字符串数据。在这篇博文中我们将学习如何在 C# 中将图像转换为 Base64。
Aspose.SVG最新下载qun666790229https://www.evget.com/product/4207/download Base64 编码是一种二进制到文本的编码方案可有效地将二进制数据转换为 ASCII 字符为数据交换提供通用格式。在某些情况下我们可能需要将JPG或PNG图像转换为 Base64 字符串数据。在这篇博文中我们将学习如何在 C# 中将图像转换为 Base64。
图像到 Base64 转换器 C# API
我们将使用Aspose.SVG for .NET API将图像转换为 Base64 。它专为处理和渲染SVG文件而设计。它允许开发人员创建、读取、更新、转换和解析 SVG 文件。
请下载API的DLL或使用NuGet安装。 PM Install-Package Aspose.SVG在 C# 中将图像转换为 Base64
我们可以按照以下步骤在 C# 中以编程方式轻松将图像转换为 Base64
加载输入 JPG 图像。创建SVGDocument类的实例。使用SVGImageElement类创建图像元素。将图像转换为 Base64。将图像元素添加到 SVG 文档中。最后调用save()方法保存SVG文档。
以下代码示例展示了如何在 C# 中将 JPG 图像转换为 Base64。 // Load an input JPG image
var bytes File.ReadAllBytes(C:\Files\Sample_JPG.jpg);// Initialize an SVGDocument object
var document new SVGDocument();// Create an image element
var img (SVGImageElement)document.CreateElementNS(http://www.w3.org/2000/svg, image);// Convert image to Base64
img.Href.BaseVal data:image/png;charsetutf-8;base64, Convert.ToBase64String(bytes);// Add the image element into the SVG document
document.RootElement.AppendChild(img);// Save the SVG document
document.Save(C:\Files\image-base64.svg);C# 中的 PNG 到 Base64
同样我们可以按照前面提到的步骤将 PNG 图像转换为 Base64。不过我们只需要输入一张PNG图片即可如下所示 // Load an input JPG image
var bytes File.ReadAllBytes(C:\Files\Sample.png);// Initialize an SVGDocument object
var document new SVGDocument();// Create an image element
var img (SVGImageElement)document.CreateElementNS(http://www.w3.org/2000/svg, image);// Convert image to Base64
img.Href.BaseVal data:image/png;charsetutf-8;base64, Convert.ToBase64String(bytes);// Add the image element into the SVG document
document.RootElement.AppendChild(img);// Save the SVG document
document.Save(C:\Files\image-base64.svg); 结论
在这篇博文中我们学习了如何在 C# 中将图像转换为 Base64 字符串。通过遵循概述的步骤您可以轻松地将图像转换功能集成到您的应用程序中~