在C#中使用imagick将.HEIC转换为JPEG [英] Converting .HEIC to JPEG using imagick in C#
本文介绍了在C#中使用imagick将.HEIC转换为JPEG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法将heic文件转换为jpeg
I'm having trouble in converting heic file to jpeg
我已经尝试过在线搜索它,我可以找到如何写一个文件夹,但是找不到如何获取转换后文件的字节[],这样我就可以保存它了
I have already tried searching it online, i can find how to write to a folder but not how to get a byte[] of a converted file so that i can save it
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);
我无法获取输出文件,它只是一个字符串
I'm not able to get the output file, it's just a string
推荐答案
根据文档,就像@SLaks建议的那样,您需要通过MemoryStream进行操作.选中此
According to the documentation, and just like @SLaks suggested, you need to do it via a MemoryStream. Check this example straight from the docs:
// 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();
}
这篇关于在C#中使用imagick将.HEIC转换为JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文