伙计我正在尝试裁剪图像并将其另存为PNG。
我可以剪裁得很好,但图片不能正确保存为PNG - 它会保存为JPEG格式或原始格式。 仅当用户决定不裁剪图像时
这就是我的意思,即使我设置ImageFormat.Png
,图像也会保存为JPEG。
以下是我正在使用的代码:
private byte[] ResizeImage2(string resizeInfo)
{
string[] picInfo = resizeInfo.Split('|');
int width = int.Parse(picInfo[0]);
int height = int.Parse(picInfo[1]);
int targetWidth = int.Parse(picInfo[2]);
int targetHeight = int.Parse(picInfo[3]);
int x = int.Parse(picInfo[4]);
int y = int.Parse(picInfo[5]);
using (var fileStore = new EPMLiveFileStore(Web))
{
using (var sourceImage = new Bitmap(fileStore.GetStream(FileNameField.Value)))
{
using (var bitmap = new Bitmap(width, height))
{
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(sourceImage, new Rectangle(0, 0, width, height));
using (var memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, ImageFormat.Png);
using (var bmp = new Bitmap(bitmap))
{
using (var pic = bmp.Clone(new Rectangle(x, y, targetWidth, targetHeight), bitmap.PixelFormat))
{
using (var stream = new MemoryStream())
{
pic.Save(stream, ImageFormat.Png);
return stream.ToArray();
}
}
}
}
}
}
}
}
}
答案 0 :(得分:1)
我对您的代码进行了一些更改。该方法返回表示为byte
数组的整个PNG文件(不确定这是否是您要实现的目标):
private byte[] ResizeImage2(string resizeInfo)
{
string[] picInfo = resizeInfo.Split('|');
int width = int.Parse(picInfo[0]);
int height = int.Parse(picInfo[1]);
int targetWidth = int.Parse(picInfo[2]);
int targetHeight = int.Parse(picInfo[3]);
int x = int.Parse(picInfo[4]);
int y = int.Parse(picInfo[5]);
byte[] rslt;
Bitmap sourceImage;
using (var fileStore = new EPMLiveFileStore(Web))
{
sourceImage = new Bitmap(fileStore.GetStream(FileNameField.Value));
}
using (var bitmap = new Bitmap(targetWidth, targetHeight))
{
bitmap.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.DrawImage(sourceImage, new Rectangle(0, 0, targetWidth, targetHeight), new Rectangle(x, y, targetWidth, targetHeight), GraphicsUnit.Pixel);
using (var memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, ImageFormat.Png);
rslt = memoryStream.ToArray();
}
}
}
return rslt;
}
由于您只是裁剪(不执行重新取样),因此可能不需要以下行:
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.SmoothingMode = SmoothingMode.HighQuality;
答案 1 :(得分:0)
采用Jigstore和简化我认为你可以使用以下内容:
private byte[] ResizeImage2(string resizeInfo)
{
string[] picInfo = resizeInfo.Split('|');
int width = int.Parse(picInfo[0]);
int height = int.Parse(picInfo[1]);
int targetWidth = int.Parse(picInfo[2]);
int targetHeight = int.Parse(picInfo[3]);
int x = int.Parse(picInfo[4]);
int y = int.Parse(picInfo[5]);
using (var fileStore = new EPMLiveFileStore(Web))
{
using (Bitmap sourceImage = new Bitmap(fileStore.GetStream(FileNameField.Value)))
{
using (var bitmap = new Bitmap(width, height))
{
bitmap.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.DrawImageUnscaled(sourceImage, Point.Empty);
using (var memoryStream = new MemoryStream())
{
bitmap.Save(memoryStream, ImageFormat.Png);
return (byte[]) memoryStream.ToArray();
}
}
}
}
}
}
顺便说一下,您将丢失原始文件中的所有元数据。这将是EXIF信息,而JPEG中的缩略图仅举几例。如果您想保留此信息,我建议您通过命令行(http://www.hugsan.com/exifutils/)使用以下工具。