aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Frontend/GUI/PictureBox.cs
blob: 24474165e6a4334b9b3b21f1a46153c896866b82 (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Engine;
using System.Drawing.Imaging;

namespace ShiftOS.Frontend.GUI
{
    public class PictureBox : Control
    {
        private System.Drawing.Image img = null;
        private ImageLayout _layout = ImageLayout.Fit;

        public ImageLayout ImageLayout
        {
            get
            {
                return _layout;
            }
            set
            {
                _layout = value;
            }
        }

        public System.Drawing.Image Image
        {
            get
            {
                return img;
            }
            set
            {
                if (img != null)
                    img.Dispose();
                img = value;
            }
        }

        protected override void OnLayout()
        {
            if (AutoSize)
            {
                Width = (img == null) ? 0 : img.Width;
                Height = (img == null) ? 0 : img.Height;
            }
        }

        public override void Paint(Graphics gfx)
        {
            if(img != null)
                switch (_layout)
                {
                    case ImageLayout.None:
                        //Just draw the image.
                        gfx.DrawImage(img, new PointF(0, 0));
                        break;
                    case ImageLayout.Stretch:
                        //Stretch the image, with no regard for aspect ratio.
                        var stretched = ResizeImage(img, Width, Height);
                        gfx.DrawImage(stretched, 0, 0);
                        break;
                    case ImageLayout.Fit:
                        //Resize image to fit the control but keep aspect ratio.
                        var fitted = FixedSize(img, Width, Height);
                        gfx.DrawImage(fitted, 0, 0);
                        break;
                    case ImageLayout.Tile:
                        //Keep original size but tile the image.

                        for(int x = 0; x < Width; x += img.Width)
                        {
                            for (int y = 0; y < Height; y += img.Height)
                            {
                                gfx.DrawImage(img, x, y);
                            }
                        }

                        break;
                }
        }

        //Again, thanks StackOverflow
        static Image FixedSize(Image imgPhoto, int Width, int Height)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

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

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = System.Convert.ToInt16((Width -
                              (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = System.Convert.ToInt16((Height -
                              (sourceHeight * nPercent)) / 2);
            }

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

            Bitmap bmPhoto = new Bitmap(Width, Height,
                              PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                             imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(SkinEngine.LoadedSkin.ControlColor);
            grPhoto.InterpolationMode =
                    InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }
    }

    public enum ImageLayout
    {
        None,
        Stretch,
        Tile,
        Fit,
    }
}