0

I have an App that sends emails. Most of the emails are 600px wide. When we create IPAD screenshots of those emails, they are created as 768px wide, therefore they have a grey frame like the following.

email We also have some wider templates, where the grey frame is just above and below the email.

Question: When generating screenshots, is it possible to identify the actual rectangle of the email, and crop the image to remove the grey frame around it?

Thanks

pedrodotnet
  • 788
  • 3
  • 16
  • 34
  • I'm not following. An email is a header (sender, receiver, some other stuff), a stream of text, and zero, one or more attachments (each of them, streams with mime-types). What kind of rectangle are you looking for? – Flydog57 Jul 11 '18 at 18:59
  • @Flydog57 I edited my post with an image, I need to delete that grey frame that appear when taking the capture – pedrodotnet Jul 11 '18 at 19:03
  • What are you using to get the screen shot? The IPad screenshot key combo or an application or code? – Warren LaFrance Jul 11 '18 at 19:07
  • It's a relatively complex problem if you want to do it without an external library. You basically need load the bitmap, find where the limit is, then only keep the relevant part, put into a new one, then save it. – Tipx Jul 11 '18 at 19:10
  • @Flydog57 we use an external service called Litmus – pedrodotnet Jul 11 '18 at 19:12
  • @tipx Is there any library that can help me on this? – pedrodotnet Jul 11 '18 at 19:15
  • Are the emails in question always like the image shown? As in, you always need to crop the same rectangle out of the "grey" container? Try this... https://stackoverflow.com/questions/734930/how-to-crop-an-image-using-c – mjw Jul 11 '18 at 19:17
  • @pedrodotnet Maybe http://www.aforgenet.com/articles/shape_checker/ ? I never used it, something like that would identify the rectangle of the email, then you crop it. – Tipx Jul 11 '18 at 19:18
  • Well in C#, it's fairly easy, especially if that grey color is consistent. You just come in from each side, and when the pixel color is no longer that gray mark where it changed. As far as tools, yes. I can recommend Atalasoft from experience. but honestly this is 5 minutes of code in c#, so I'd just tackle it yourself. – Trey Jul 11 '18 at 19:30
  • @tipx I agree, I forgot about aforge, I used that for a scanner device project, it was top-notch. – Trey Jul 11 '18 at 19:31
  • @trey I like what you said could you explain more how to do it with C#? – pedrodotnet Jul 11 '18 at 19:32
  • IIUC, part of your system creates a problem (gray border area). I see two solutions: 1. Add another moving part to system to remove the gray border area. 2. Fix the existing part of your system that is generating the gray box. Have you considered option 2? – Jeffrey Rennie Jul 11 '18 at 23:36
  • @JeffreyRennie I think it's something that is created by default. My system is not generating any gray frames – pedrodotnet Jul 12 '18 at 13:26

2 Answers2

1

I do not have time to write a full solution, but here is how to traverse the bitmap, and check the colors.

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
        }
    }
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Trey
  • 413
  • 4
  • 12
0

I'm sharing with you the solution I came up with. I hope that helps anybody. I created this helper that receives the image as a byte array and the color of the frame. Then it creates the frame limits and uses these values to create another image without the frame.

 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();
        }
    }
}
pedrodotnet
  • 788
  • 3
  • 16
  • 34