在我的ASP.NET C#项目中,我正在使用MagickNet进行图像处理。我的问题是,我上传了一个透明的PNG图像,当我将它转换为JPEG时,我得到了一个带有一些白点的黑色背景,而不是透明部分的白色背景。
Stream su = upload.FileContent;
MagickNet.Image testimage = new MagickNet.Image(su);
testimage.Filter = FilterType.LanczosFilter;
testimage.Compression = CompressionType.JPEGCompression;
testimage.QuantizeDither = false;
testimage.BackgroundColor = new Color(System.Drawing.Color.White);
testimage.Resize( new System.Drawing.Size(Convert.ToInt32(testimage.Size.Width * 0.4), Convert.ToInt32(testimage.Size.Height * 0.4)));
testimage.Write(System.Web.HttpContext.Current.Server.MapPath(".") + "\\temp\\" + DateTime.Now.Hour + "-" +DateTime.Now.Minute + "-" + DateTime.Now.Second + ".jpg");
su.Close();
su.Dispose();
testimage.Dispose();
Magick.Term();我玩了它,总是得到错误的结果,我追求。有时我得到一个透明的背景,但图像外部区域的一些部分有白点。我还调整了图像的大小,使其小于实际大小。我认为是重新调整大小导致了问题。
更新:这是由于某些原因导致的大小调整。不需要调整大小,它就能正常工作。话虽如此,我需要调整大小,所以我需要它与它一起工作。
谢谢。
发布于 2015-08-11 01:53:40
尝试合成到白色背景图像上。
Image bg = new Image(testimage.Size, new ColorRGB(255, 255, 255));
testimage = bg.Composite(testimage, 0, 0);发布于 2016-07-23 00:56:46
首先,最好创建具有所需大小的MagickImage对象,在某些情况下,读取所需大小的文件/流的速度可以快100倍。你可能不会有这个错误。
using(var testimage = new MagickImage(yourstream/yourFileAddress, width, height)
{
....
}但是,如果您将MagickImage转换为位图,然后将位图另存为jpg,则可以看到图像具有白色背景
using (var testBitmap = testimage.ToBitmap())
{
testBitmap.Save(@"d:\temp.jpg");
}而且,使用比调用dispose成员函数要好得多。因为如果您的代码在到达dispose调用之前抛出异常,那么您的对象将保留在内存中。但是使用的时候,如果程序跳出了代码块,对象就会被释放。
https://stackoverflow.com/questions/15267576
复制相似问题