DisplayMagician/DisplayMagicianShared/ProfileIcon.cs

419 lines
15 KiB
C#
Raw Permalink Normal View History

2017-02-26 19:23:31 +00:00
using System;
using System.Collections.Generic;
2017-02-26 19:23:31 +00:00
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.IconLib;
using System.Drawing.Imaging;
using System.Linq;
namespace DisplayMagicianShared
2017-02-26 19:23:31 +00:00
{
public class ProfileIcon
{
private ProfileItem _profile;
public ProfileIcon(ProfileItem profile, int paddingX = 100, int paddingY = 100)
2017-02-26 19:23:31 +00:00
{
_profile = profile;
2017-02-26 19:23:31 +00:00
PaddingX = paddingX;
PaddingY = paddingY;
}
public int PaddingX { get; }
public int PaddingY { get; }
//public Profile Profile { get; }
2017-02-26 19:23:31 +00:00
2018-10-20 00:08:30 +00:00
// ReSharper disable once TooManyArguments
2017-02-26 19:23:31 +00:00
public static RectangleF CalculateViewSize(
List<ScreenPosition> screens,
int outsidePaddingSides = 0,
int outsidePaddingTopBottom = 0)
2017-02-26 19:23:31 +00:00
{
var minX = 0;
var maxX = 0;
var minY = 0;
var maxY = 0;
2018-10-20 00:27:25 +00:00
// If there aren't any screens provided then return a blank rectangle
if (screens.Count == 0)
{
return new RectangleF(0, 0, 0, 0);
}
foreach (var screen in screens)
2017-02-26 19:23:31 +00:00
{
//var res = GetLargestResolution(screens);
minX = Math.Min(minX, screen.ScreenX);
maxX = Math.Max(maxX, screen.ScreenX + screen.ScreenWidth);
minY = Math.Min(minY, screen.ScreenY);
maxY = Math.Max(maxY, screen.ScreenY + screen.ScreenHeight);
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
if (outsidePaddingSides != 0)
2017-02-26 19:23:31 +00:00
{
minX -= outsidePaddingSides;
maxX += outsidePaddingSides;
}
if (outsidePaddingTopBottom != 0)
{
minY -= outsidePaddingTopBottom;
maxY += outsidePaddingTopBottom;
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
var width = Math.Abs(maxX - minX);
var height = Math.Abs(maxY - minY);
var size = new SizeF(width, height);
2017-02-26 19:23:31 +00:00
var rect = new RectangleF(new PointF(minX, minY), size);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return rect;
}
2017-02-26 19:23:31 +00:00
/// <summary>
/// Creates a rounded rectangle path
/// By @taffer
/// https://stackoverflow.com/questions/33853434/how-to-draw-a-rounded-rectangle-in-c-sharp
/// </summary>
public static GraphicsPath RoundedRect(RectangleF bounds, float radius)
{
2018-10-20 00:27:25 +00:00
var diameter = radius * 2;
2017-02-26 19:23:31 +00:00
var size = new SizeF(diameter, diameter);
var arc = new RectangleF(bounds.Location, size);
var path = new GraphicsPath();
if (radius < 0.01)
{
path.AddRectangle(bounds);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return path;
}
// top left arc
path.AddArc(arc, 180, 90);
// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);
// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);
// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return path;
}
public Bitmap ToBitmap(int width = 256, int height = 256, PixelFormat format = PixelFormat.Format32bppArgb)
2017-02-26 19:23:31 +00:00
{
var bitmap = new Bitmap(width, height, format);
bitmap.MakeTransparent();
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
using (var g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.HighQuality;
DrawDisplayLayout(g, width, height);
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return bitmap;
}
public Bitmap ToTightestBitmap(PixelFormat format = PixelFormat.Format32bppArgb)
2017-02-26 19:23:31 +00:00
{
if (_profile.Screens.Count == 0)
{
return new Bitmap(0, 0);
}
var viewSize = CalculateViewSize(_profile.Screens, 0, 0);
var bitmap = new Bitmap((int)viewSize.Width, (int)viewSize.Height, format);
bitmap.MakeTransparent();
using (var g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.HighQuality;
DrawDisplayLayout(g, (int)viewSize.Width, (int)viewSize.Height, false);
}
return bitmap;
}
public Bitmap ToTightestBitmap(int width, int height, PixelFormat format = PixelFormat.Format32bppArgb)
{
if (_profile.Screens.Count == 0)
{
return new Bitmap(0, 0);
}
var bitmap = new Bitmap(width, height, format);
bitmap.MakeTransparent();
using (var g = Graphics.FromImage(bitmap))
2017-02-26 19:23:31 +00:00
{
g.SmoothingMode = SmoothingMode.HighQuality;
DrawDisplayLayout(g, width, height, false);
}
return bitmap;
}
/*public Bitmap ToBitmapOverlay(Bitmap bitmap)
{
if (_profile.Screens.Count == 0)
{
return new Bitmap(0, 0);
}
var viewSize = CalculateViewSize(_profile.Screens, PaddingX, PaddingY);
var width = bitmap.Width * 0.7f;
var height = width / viewSize.Width * viewSize.Height;
using (var g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.TranslateTransform(bitmap.Width - width, bitmap.Height - height * 1.1f);
DrawDisplayLayout(g, width, height);
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return bitmap;
}*/
2017-02-26 19:23:31 +00:00
public MultiIcon ToIcon()
{
var iconSizes = new[]
{
new Size(256, 256),
new Size(64, 64),
new Size(48, 48),
new Size(32, 32),
new Size(24, 24),
new Size(16, 16)
};
var multiIcon = new MultiIcon();
var icon = multiIcon.Add("Icon1");
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
foreach (var size in iconSizes)
{
icon.Add(ToBitmap(size.Width, size.Height));
2018-10-20 00:27:25 +00:00
if (size.Width >= 256 && size.Height >= 256)
{
2017-02-26 19:23:31 +00:00
icon[icon.Count - 1].IconImageFormat = IconImageFormat.PNG;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
multiIcon.SelectedIndex = 0;
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return multiIcon;
}
/*public MultiIcon ToIconOverlay(string iconAddress)
2017-02-26 19:23:31 +00:00
{
var multiIcon = new MultiIcon();
var icon = multiIcon.Add("Icon1");
var mainIcon = new MultiIcon();
mainIcon.Load(iconAddress);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
foreach (var singleIcon in mainIcon[0].Where(image =>
2018-10-20 00:27:25 +00:00
image.PixelFormat == PixelFormat.Format16bppRgb565 ||
image.PixelFormat == PixelFormat.Format24bppRgb ||
image.PixelFormat == PixelFormat.Format32bppArgb)
2017-02-26 19:23:31 +00:00
.OrderByDescending(
image =>
image.PixelFormat == PixelFormat.Format16bppRgb565
? 1
2018-10-20 00:27:25 +00:00
: image.PixelFormat == PixelFormat.Format24bppRgb
? 2
: 3)
.ThenByDescending(image => image.Size.Width * image.Size.Height))
2017-02-26 19:23:31 +00:00
{
2018-10-20 00:27:25 +00:00
if (!icon.All(i => singleIcon.Size != i.Size || singleIcon.PixelFormat != i.PixelFormat))
{
2017-02-26 19:23:31 +00:00
continue;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
var bitmap = singleIcon.Icon.ToBitmap();
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (bitmap.PixelFormat != singleIcon.PixelFormat)
{
var clone = new Bitmap(bitmap.Width, bitmap.Height, singleIcon.PixelFormat);
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
using (var gr = Graphics.FromImage(clone))
{
gr.DrawImage(bitmap, new Rectangle(0, 0, clone.Width, clone.Height));
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
bitmap.Dispose();
bitmap = clone;
}
2018-10-20 00:27:25 +00:00
icon.Add(singleIcon.Size.Height * singleIcon.Size.Width < 24 * 24 ? bitmap : ToBitmapOverlay(bitmap));
2018-10-20 00:27:25 +00:00
if (singleIcon.Size.Width >= 256 && singleIcon.Size.Height >= 256)
{
2017-02-26 19:23:31 +00:00
icon[icon.Count - 1].IconImageFormat = IconImageFormat.PNG;
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
bitmap.Dispose();
}
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
if (icon.Count == 0)
2018-10-20 00:27:25 +00:00
{
2017-02-26 19:23:31 +00:00
throw new ArgumentException();
2018-10-20 00:27:25 +00:00
}
2017-02-26 19:23:31 +00:00
multiIcon.SelectedIndex = 0;
2018-10-20 00:27:25 +00:00
2017-02-26 19:23:31 +00:00
return multiIcon;
}*/
2017-02-26 19:23:31 +00:00
private void DrawDisplayLayout(Graphics g, float maxWidth, float maxHeight, bool drawStand = true)
{
// If we don't have any screens, then we can't draw them!
if (_profile.Screens.Count == 0)
{
return;
}
// Figure out the sizes we need based on the total size of the screens
var viewSize = ProfileIcon.CalculateViewSize(_profile.Screens, PaddingX, PaddingY);
var standPadding = maxHeight * 0.005f;
maxHeight -= standPadding * 8;
var factor = Math.Min((maxWidth - 2 * standPadding - 1) / viewSize.Width,
(maxHeight - 2 * standPadding - 1) / viewSize.Height);
g.ScaleTransform(factor, factor);
// Make space for the stand
var xOffset = ((maxWidth - 1) / factor - viewSize.Width) / 2f;
var yOffset = ((maxHeight - 1) / factor - viewSize.Height) / 2f;
g.TranslateTransform(-viewSize.X + xOffset, -viewSize.Y + yOffset);
// How wide the Bezel is on the screen graphics
int screenBezel = 60;
//int screenWordBuffer = 30;
// Draw the stand if needed
if (drawStand)
{
if (standPadding * 6 >= 1)
{
using (
var boundRect =
RoundedRect(
new RectangleF(viewSize.Width * 0.375f + viewSize.X,
viewSize.Height + standPadding / factor,
viewSize.Width / 4, standPadding * 7 / factor), 2 * standPadding / factor))
{
g.FillPath(new SolidBrush(Color.FromArgb(250, 50, 50, 50)), boundRect);
g.DrawPath(new Pen(Color.FromArgb(50, 255, 255, 255), 2 / factor), boundRect);
}
}
else
{
g.FillRectangle(new SolidBrush(Color.FromArgb(200, 255, 255, 255)), viewSize);
g.DrawRectangle(new Pen(Color.FromArgb(170, 50, 50, 50), standPadding / factor), viewSize.X, viewSize.Y,
viewSize.Width, viewSize.Height);
}
}
// Now go through and draw the screens
foreach (ScreenPosition screen in _profile.Screens)
{
2022-02-05 08:28:51 +00:00
Rectangle outlineRect;
Rectangle screenRect;
// draw the screen
if (screen.IsSpanned)
{
// We do these things only if the screen IS spanned!
// Draw the outline of the spanned monitor
2022-02-05 08:28:51 +00:00
outlineRect = new Rectangle(screen.ScreenX, screen.ScreenY, screen.ScreenWidth, screen.ScreenHeight);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 33, 33, 33)), outlineRect);
g.DrawRectangle(Pens.Black, outlineRect);
// Draw the screen of the monitor
2022-02-05 08:28:51 +00:00
screenRect = new Rectangle(screen.ScreenX + screenBezel, screen.ScreenY + screenBezel, screen.ScreenWidth - (screenBezel * 2), screen.ScreenHeight - (screenBezel * 2));
g.FillRectangle(new SolidBrush(screen.Colour), screenRect);
g.DrawRectangle(Pens.Black, screenRect);
}
else
{
// Draw the outline of the monitor
2022-02-05 08:28:51 +00:00
outlineRect = new Rectangle(screen.ScreenX, screen.ScreenY, screen.ScreenWidth, screen.ScreenHeight);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 33, 33, 33)), outlineRect);
g.DrawRectangle(Pens.Black, outlineRect);
// Draw the screen of the monitor
2022-02-05 08:28:51 +00:00
screenRect = new Rectangle(screen.ScreenX + screenBezel, screen.ScreenY + screenBezel, screen.ScreenWidth - (screenBezel * 2), screen.ScreenHeight - (screenBezel * 2));
g.FillRectangle(new SolidBrush(screen.Colour), screenRect);
g.DrawRectangle(Pens.Black, screenRect);
}
2022-02-05 08:28:51 +00:00
// Draw the location of the taskbar for this screen
Rectangle taskBarRect;
//Rectangle startButtonRect;
int taskBarWidth = (int)(0.15 * screenRect.Height);
//int startButtonSpacer = (int)(0.25 * taskBarWidth);
//int startButtonSize = 2 * startButtonSpacer;
switch (screen.TaskBarEdge)
{
2022-03-29 21:56:24 +00:00
case Windows.TaskBarLayout.TaskBarEdge.Left:
2022-02-05 08:28:51 +00:00
taskBarRect = new Rectangle(screenRect.X, screenRect.Y + 2, taskBarWidth, screenRect.Height - 4);
break;
2022-03-29 21:56:24 +00:00
case Windows.TaskBarLayout.TaskBarEdge.Top:
2022-02-05 08:28:51 +00:00
taskBarRect = new Rectangle(screenRect.X + 2, screenRect.Y, screenRect.Width - 4, taskBarWidth);
break;
2022-03-29 21:56:24 +00:00
case Windows.TaskBarLayout.TaskBarEdge.Right:
2022-02-05 08:28:51 +00:00
taskBarRect = new Rectangle(screenRect.X + screenRect.Width - taskBarWidth, screenRect.Y + 2, taskBarWidth, screenRect.Height - 4);
break;
2022-03-29 21:56:24 +00:00
case Windows.TaskBarLayout.TaskBarEdge.Bottom:
2022-02-05 08:28:51 +00:00
taskBarRect = new Rectangle(screenRect.X + 2, screenRect.Y + screenRect.Height - taskBarWidth, screenRect.Width - 4, taskBarWidth);
break;
default:
taskBarRect = new Rectangle(screenRect.X + 2, screenRect.Y + screenRect.Height - taskBarWidth, screenRect.Width - 4, taskBarWidth);
break;
}
g.FillRectangle(new SolidBrush(Color.FromArgb(255, 200, 200, 200)), taskBarRect);
}
}
private Color pickTextColorBasedOnBgColour(Color bgColour, Color lightColour, Color darkColour)
{
if ((bgColour.R * 0.299 + bgColour.G * 0.587 + bgColour.B * 0.114) > 186)
{
return darkColour;
}
else
{
return lightColour;
}
}
private Bitmap pickBitmapBasedOnBgColour(Color bgColour, Bitmap lightBitmap, Bitmap darkBitmap)
{
if ((bgColour.R * 0.299 + bgColour.G * 0.587 + bgColour.B * 0.114) > 186)
{
return darkBitmap;
}
else
{
return lightBitmap;
}
2017-02-26 19:23:31 +00:00
}
}
}