在 C# 中使用 imagick 将 .HEIC 转换为 JPEG
Converting .HEIC to JPEG using imagick in C#
我在将 heic 文件转换为 jpeg 时遇到问题
我已经尝试在线搜索它,我可以找到如何写入文件夹但无法找到如何获取转换后文件的字节[]以便我可以保存它
byte[] file = null;
file = Convert.FromBase64String(dto.File);
//Convert HEIC/HEIF to JPF
if (extension == "HEIC" || extension == "HEIF")
{
try
{
using (MagickImageCollection images = new MagickImageCollection())
{
images.Read(file);
using (IMagickImage vertical = images.AppendVertically())
{
var imgname = filename + ".jpeg";
vertical.Format = MagickFormat.Jpeg;
vertical.Density = new Density(300);
vertical.Write(imgname);
extension = "jpeg";
}
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
}
documentId = Service.AddSupportingDocument(file, extension , userName);
我无法获取输出文件,它只是一个字符串
您需要创建一个 MemoryStream
,调用 .Write()
将图像写入内存流,然后在流上调用 .ToArray()
以获取它写入的字节。
根据文档,就像@SLaks 建议的那样,您需要通过 MemoryStream 来完成。直接从文档中检查 example:
// Read first frame of gif image
using (MagickImage image = new MagickImage("Snakeware.gif"))
{
// Save frame as jpg
image.Write("Snakeware.jpg");
}
// Write to stream
MagickReadSettings settings = new MagickReadSettings();
// Tells the xc: reader the image to create should be 800x600
settings.Width = 800;
settings.Height = 600;
using (MemoryStream memStream = new MemoryStream())
{
// Create image that is completely purple and 800x600
using (MagickImage image = new MagickImage("xc:purple", settings))
{
// Sets the output format to png
image.Format = MagickFormat.Png;
// Write the image to the memorystream
image.Write(memStream);
}
}
// Read image from file
using (MagickImage image = new MagickImage("Snakeware.png"))
{
// Sets the output format to jpeg
image.Format = MagickFormat.Jpeg;
// Create byte array that contains a jpeg file
byte[] data = image.ToByteArray();
}
其他解决方案。
适用于 dotnet 6 & Nuget 包 Magick.NET-Q16-AnyCPU
string[] allfiles = Directory.GetFiles(@"__PATH__", "*.heic", SearchOption.AllDirectories);
foreach (var file in allfiles)
{
FileInfo info = new FileInfo(file);
using (MagickImage image = new MagickImage(info.FullName))
{
// Save frame as jpg
image.Write(@$"__OUTPATH__{info.Name}.jpg");
}
}
我在将 heic 文件转换为 jpeg 时遇到问题
我已经尝试在线搜索它,我可以找到如何写入文件夹但无法找到如何获取转换后文件的字节[]以便我可以保存它
byte[] file = null;
file = Convert.FromBase64String(dto.File);
//Convert HEIC/HEIF to JPF
if (extension == "HEIC" || extension == "HEIF")
{
try
{
using (MagickImageCollection images = new MagickImageCollection())
{
images.Read(file);
using (IMagickImage vertical = images.AppendVertically())
{
var imgname = filename + ".jpeg";
vertical.Format = MagickFormat.Jpeg;
vertical.Density = new Density(300);
vertical.Write(imgname);
extension = "jpeg";
}
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
}
documentId = Service.AddSupportingDocument(file, extension , userName);
我无法获取输出文件,它只是一个字符串
您需要创建一个 MemoryStream
,调用 .Write()
将图像写入内存流,然后在流上调用 .ToArray()
以获取它写入的字节。
根据文档,就像@SLaks 建议的那样,您需要通过 MemoryStream 来完成。直接从文档中检查 example:
// Read first frame of gif image
using (MagickImage image = new MagickImage("Snakeware.gif"))
{
// Save frame as jpg
image.Write("Snakeware.jpg");
}
// Write to stream
MagickReadSettings settings = new MagickReadSettings();
// Tells the xc: reader the image to create should be 800x600
settings.Width = 800;
settings.Height = 600;
using (MemoryStream memStream = new MemoryStream())
{
// Create image that is completely purple and 800x600
using (MagickImage image = new MagickImage("xc:purple", settings))
{
// Sets the output format to png
image.Format = MagickFormat.Png;
// Write the image to the memorystream
image.Write(memStream);
}
}
// Read image from file
using (MagickImage image = new MagickImage("Snakeware.png"))
{
// Sets the output format to jpeg
image.Format = MagickFormat.Jpeg;
// Create byte array that contains a jpeg file
byte[] data = image.ToByteArray();
}
其他解决方案。
适用于 dotnet 6 & Nuget 包 Magick.NET-Q16-AnyCPU
string[] allfiles = Directory.GetFiles(@"__PATH__", "*.heic", SearchOption.AllDirectories);
foreach (var file in allfiles)
{
FileInfo info = new FileInfo(file);
using (MagickImage image = new MagickImage(info.FullName))
{
// Save frame as jpg
image.Write(@$"__OUTPATH__{info.Name}.jpg");
}
}