我有一个发送电子邮件的应用程序。大多数电子邮件的宽度为600像素。当我们为这些电子邮件创建IPAD屏幕截图时,它们被创建为768px宽,因此它们具有如下灰色框。
问题: 生成屏幕截图时,是否可以识别电子邮件的实际矩形并裁剪图像以去除其周围的灰色框?
谢谢
答案 0 :(得分:1)
我没有时间编写完整的解决方案,但是这里是如何遍历位图并检查颜色的方法。
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100);
for (int x=0;x<= bmp.Width;x++)
{
//just an example, you will have to use the actual color values
System.Drawing.Color myBorderColor = System.Drawing.Color.Gray;
for (int y = 0; y <= bmp.Width; y++)
{
var pixel = bmp.GetPixel(x, y);
if (pixel!= myBorderColor)
{
//we have hit something that is not your border, record it.
//so just mark down in a list or whatever where the grey is vs not
}
}
}
答案 1 :(得分:0)
我正在与您分享我想出的解决方案。希望对大家有帮助。我创建了这个帮助程序,它以字节数组和框架的颜色接收图像。然后,它创建帧限制,并使用这些值来创建没有帧的另一张图像。
public class ImageHelper
{
public static byte[] CropImage(byte[] sourceImage, Color grey)
{
using (var ms = new MemoryStream(sourceImage))
{
var Img = new Bitmap(ms);
using (Bitmap bmp = new Bitmap(Img))
{
var midX = bmp.Width / 2;
var midY = bmp.Height / 2;
var yTop = 0;
var yBottom = bmp.Height;
var xLeft = 0;
var xRight = bmp.Width;
for (int y = 0; y < bmp.Height; y++)
{
Color pxl = bmp.GetPixel(midX, y);
if (pxl != grey)
{
yTop = y;
break;
}
}
for (int x = 0; x < bmp.Width; x++)
{
Color pxl = bmp.GetPixel(x, midX);
if (pxl != grey)
{
xLeft = x;
break;
}
}
for (int x = bmp.Width - 1; x > midX; x--)
{
Color pxl = bmp.GetPixel(x, midX);
if (pxl != grey)
{
xRight = x;
break;
}
}
for (int y = bmp.Height - 1; y > midY; y--)
{
Color pxl = bmp.GetPixel(midX, y);
if (pxl != grey)
{
yBottom = y;
break;
}
}
Image redBmp = bmp.Clone(new Rectangle(xLeft, yTop, xRight - xLeft, yBottom - yTop), System.Drawing.Imaging.PixelFormat.DontCare);
byte[] byteImage = ImageToByteArray(redBmp);
return byteImage;
}
}
}
public static byte[] ImageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
return ms.ToArray();
}
}
}