This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModPicture.cs
123 lines (102 loc) · 4.13 KB
/
ModPicture.cs
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
//
// Mark König, 03/2022
//
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace LoadBingPicture
{
public class ModPicture
{
public static Image ChangePicture(Image original, int resolution, LoadJson.BingPicture picture)
{
// load original image
Image image = new Bitmap(original);
// rezize to the new size
Rectangle ScreenResolution = Screen.PrimaryScreen.Bounds;
Image image1 = FixedSize(image, ScreenResolution.Width, ScreenResolution.Height);
// dispose the original
image.Dispose();
// load the back gfx
Image image2 = new Bitmap(Properties.Resources.back75);
// we want to cover 1/10 of the back
int back = image1.Height / 10;
using (Graphics gr = Graphics.FromImage(image1))
{
// we have 2 line and 3 spaces
Font stringFont = new Font("Arial", back / 5);
// measure the size
SizeF l1 = gr.MeasureString(picture.title, stringFont);
string desc = picture.description;
SizeF l2 = gr.MeasureString(desc, stringFont);
while (l2.Width > (image1.Width - 100))
{
desc = desc.Substring(0, desc.Length - 8);
desc = desc + "...";
l2 = gr.MeasureString(desc, stringFont);
}
float l = l1.Width;
if (l2.Width > l) l = l2.Width;
int yValue = (int)l1.Height / 2;
int x = image1.Width - (int)l - 50;
gr.DrawImage(image2,
new Rectangle(new Point(x, back * 8),
new Size((int)l + 20, (3 * yValue) + (2 * (int)l1.Height))));
gr.DrawString(picture.title,
stringFont,
new SolidBrush(Color.White),
new PointF(x + 10, back * 8 + yValue));
gr.DrawString(desc,
stringFont,
new SolidBrush(Color.White),
new PointF(x + 10, back * 8 + yValue + (int)l1.Height + yValue));
}
// dispose the back gfx
image2.Dispose();
return image1;
}
public 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(Color.Black);
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;
}
}
}