2016-07-09 22:30:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
2017-02-01 03:35:18 +00:00
|
|
|
|
namespace PKHeX.WinForms
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
2017-01-12 01:55:42 +00:00
|
|
|
|
public static class ImageUtil
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
|
|
|
|
// Image Layering/Blending Utility
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static Bitmap LayerImage(Image baseLayer, Image overLayer, int x, int y, double trans)
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
2016-12-16 07:17:17 +00:00
|
|
|
|
if (baseLayer == null)
|
|
|
|
|
return overLayer as Bitmap;
|
2016-07-09 22:30:12 +00:00
|
|
|
|
Bitmap img = new Bitmap(baseLayer.Width, baseLayer.Height);
|
|
|
|
|
using (Graphics gr = Graphics.FromImage(img))
|
|
|
|
|
{
|
|
|
|
|
gr.DrawImage(baseLayer, new Point(0, 0));
|
2017-08-13 07:21:42 +00:00
|
|
|
|
Image o = trans == 1f ? overLayer : ChangeOpacity(overLayer, trans);
|
2016-07-09 22:30:12 +00:00
|
|
|
|
gr.DrawImage(o, new Rectangle(x, y, overLayer.Width, overLayer.Height));
|
|
|
|
|
}
|
|
|
|
|
return img;
|
|
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static Bitmap ChangeOpacity(Image img, double trans)
|
2016-07-09 22:30:12 +00:00
|
|
|
|
{
|
|
|
|
|
if (img == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (img.PixelFormat.HasFlag(PixelFormat.Indexed))
|
|
|
|
|
return (Bitmap)img;
|
|
|
|
|
|
|
|
|
|
Bitmap bmp = (Bitmap)img.Clone();
|
|
|
|
|
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
|
|
|
|
IntPtr ptr = bmpData.Scan0;
|
|
|
|
|
|
|
|
|
|
int len = bmp.Width * bmp.Height * 4;
|
|
|
|
|
byte[] data = new byte[len];
|
|
|
|
|
|
|
|
|
|
Marshal.Copy(ptr, data, 0, len);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.Length; i += 4)
|
|
|
|
|
data[i + 3] = (byte)(data[i + 3] * trans);
|
|
|
|
|
|
|
|
|
|
Marshal.Copy(data, 0, ptr, len);
|
|
|
|
|
bmp.UnlockBits(bmpData);
|
|
|
|
|
|
2016-10-31 02:15:48 +00:00
|
|
|
|
return bmp;
|
|
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
public static Bitmap ChangeAllColorTo(Image img, Color c)
|
2016-10-31 02:15:48 +00:00
|
|
|
|
{
|
|
|
|
|
if (img == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (img.PixelFormat.HasFlag(PixelFormat.Indexed))
|
|
|
|
|
return (Bitmap)img;
|
|
|
|
|
|
|
|
|
|
Bitmap bmp = (Bitmap)img.Clone();
|
|
|
|
|
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
|
|
|
|
IntPtr ptr = bmpData.Scan0;
|
|
|
|
|
|
|
|
|
|
int len = bmp.Width * bmp.Height * 4;
|
|
|
|
|
byte[] data = new byte[len];
|
|
|
|
|
|
|
|
|
|
Marshal.Copy(ptr, data, 0, len);
|
|
|
|
|
|
|
|
|
|
byte R = c.R;
|
|
|
|
|
byte G = c.G;
|
|
|
|
|
byte B = c.B;
|
|
|
|
|
for (int i = 0; i < data.Length; i += 4)
|
|
|
|
|
if (data[i + 3] != 0)
|
|
|
|
|
{
|
|
|
|
|
data[i + 0] = B;
|
|
|
|
|
data[i + 1] = G;
|
|
|
|
|
data[i + 2] = R;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Marshal.Copy(data, 0, ptr, len);
|
|
|
|
|
bmp.UnlockBits(bmpData);
|
|
|
|
|
|
2017-06-18 04:49:14 +00:00
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
public static Bitmap ToGrayscale(Image img)
|
|
|
|
|
{
|
|
|
|
|
if (img == null)
|
|
|
|
|
return null;
|
|
|
|
|
if (img.PixelFormat.HasFlag(PixelFormat.Indexed))
|
|
|
|
|
return (Bitmap)img;
|
|
|
|
|
|
|
|
|
|
Bitmap bmp = (Bitmap)img.Clone();
|
|
|
|
|
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
|
|
|
|
|
IntPtr ptr = bmpData.Scan0;
|
|
|
|
|
|
|
|
|
|
int len = bmp.Width * bmp.Height * 4;
|
|
|
|
|
byte[] data = new byte[len];
|
|
|
|
|
|
|
|
|
|
Marshal.Copy(ptr, data, 0, len);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < data.Length; i += 4)
|
|
|
|
|
if (data[i + 3] != 0)
|
|
|
|
|
{
|
|
|
|
|
byte greyS = (byte)((0.3 * data[i + 2] + 0.59 * data[i + 1] + 0.11 * data[i + 0]) / 3);
|
|
|
|
|
data[i + 0] = greyS;
|
|
|
|
|
data[i + 1] = greyS;
|
|
|
|
|
data[i + 2] = greyS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Marshal.Copy(data, 0, ptr, len);
|
|
|
|
|
bmp.UnlockBits(bmpData);
|
|
|
|
|
|
2016-07-09 22:30:12 +00:00
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|