Monday, March 12, 2012

Image Processing

I few weeks ago  I was  played with  image  processing.  Actually I  have a dream, I want  write super smart  application,  for image processing. But today, I'll talk about very simple   image modifications.
As I said, I few weeks  ago  I got  tons of   images,  that  I have to  update them with some sort of template.
So I wrote small Lib  and Console application for this purpose.
Result:

Original image:

Here is Image Processing Lib source code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
 public class EditImage
    {
        public Image cropImage(Image img, Rectangle cropArea)
        {
            Bitmap bmpImage = new Bitmap(img);
            Bitmap bmpCrop = bmpImage.Clone(cropArea,
                                            bmpImage.PixelFormat);
            return (Image)(bmpCrop);
        }

        public Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }

        public void saveJpeg(string path, Bitmap img, long quality)
        {
            // Encoder parameter for image quality
            EncoderParameter qualityParam =
                new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

            // Jpeg image codec
            ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");

            if (jpegCodec == null)
                return;

            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            img.Save(path, jpegCodec, encoderParams);
        }

        public ImageCodecInfo getEncoderInfo(string mimeType)
        {
            // Get image codecs for all image formats
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

            // Find the correct image codec
            for (int i = 0; i < codecs.Length; i++)
                if (codecs[i].MimeType == mimeType)
                    return codecs[i];
            return null;
        }

        public Image RoundCorners(Image StartImage, int CornerRadius, Color BackgroundColor)
        {
            CornerRadius *= 2;
            Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height);
            Graphics g = Graphics.FromImage(RoundedImage);
            g.Clear(BackgroundColor);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Brush brush = new TextureBrush(StartImage);
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90);
            gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90);
            gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
            gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
            g.FillPath(brush, gp);
            return RoundedImage;
        }

    }

Here is Console Application with  Image processing invocation :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
static void Main(string[] args)
        {
            string[] filePaths = Directory.GetFiles(@"C:\projects\ProjectX\ImageProcessing\testProcessing\bin\Debug\input\", "*.jpg");
            Image workedImage = null;
            Image template = null;
            ImageProcessing.EditImage editImage = new ImageProcessing.EditImage();
            for (int i = 0; i < filePaths.Length; i++)
            {
                try
                {
                    workedImage = Image.FromFile(filePaths[i]);//("nissan-370z-rc-1.jpg");
                    template = Image.FromFile("template_glass_orb.png");

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadKey();
                }
                if (workedImage.Width < workedImage.Height || workedImage.Width < 300)
                {
                    workedImage.Dispose();
                    template.Dispose();
                    continue;
                }

                else if (workedImage.Width >= 300)
                {
                    workedImage = editImage.resizeImage(workedImage, new Size(300, 300));
                    workedImage = editImage.cropImage(workedImage, new Rectangle(0, 0, 200, workedImage.Height));
                    if (workedImage.Height > 200)
                    {
                        workedImage = editImage.cropImage(workedImage, new Rectangle(0, 0, workedImage.Width, 195));
                    }
                    workedImage = editImage.RoundCorners(workedImage, 100, Color.Transparent);

                    if (workedImage.Width < 200)
                    {
                        workedImage.Dispose();
                        template.Dispose();
                        Console.WriteLine("Oops, we are here");
                        Console.ReadKey();
                        continue;
                    }
                }



                using (var bitmap = new Bitmap(workedImage.Width, workedImage.Height))
                {
                    using (var canvas = Graphics.FromImage(bitmap))
                    {
                        canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        canvas.DrawImage(workedImage, new Rectangle(0, 0, workedImage.Width, workedImage.Height), new Rectangle(0, 0, workedImage.Width, workedImage.Height), GraphicsUnit.Pixel);

                        //modifiedImage = RoundCorners(disairedImage, 80, Color.Transparent);
                        canvas.DrawImage(template, 0, 0, template.Width, template.Height);
                        canvas.Save();
                    }
                    try
                    {
                        bitmap.Save(@"C:\projects\ProjectX\ImageProcessing\testProcessing\bin\Debug\input\new\" + Path.GetFileName(filePaths[i]), ImageFormat.Png);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);

                    }
                }
                workedImage.Dispose();
                template.Dispose();
            }
            Console.WriteLine("done");
            Console.ReadKey();
}

As you can see, it's  very easy to use.

No comments:

Post a Comment