Make a function to hopefully make something look burnt if it is burnt.

This commit is contained in:
Tim Young 2018-02-14 11:25:32 +00:00
parent 78c9c5472f
commit de0e9b4b98

View File

@ -692,6 +692,59 @@ namespace EduNetworkBuilder
return b;
}
/// <summary>
/// We concoct a burn image overlay the same shape of the object we are drawing. Loop through the image
/// and turn the burn-image transparent everywhere the original image is transparent. Then, draw the
/// shaped burn-image on top of the original image.
/// </summary>
/// <param name="BaseImage"></param>
/// <returns>A burnt-looking network device.</returns>
public Image BurnedImage(Image BaseImage)
{
Bitmap b = new Bitmap(BaseImage);
//Make a burn image that is the same size as the base image
Bitmap Burn = new Bitmap(b.Width, b.Height);
Graphics.FromImage(Burn).DrawImage(Properties.Resources.BurnMark, new Rectangle(0, 0, b.Width, b.Height));
BitmapData bData = b.LockBits(new Rectangle(0, 0, BaseImage.Width, BaseImage.Height), ImageLockMode.ReadWrite, b.PixelFormat);
BitmapData burnData = Burn.LockBits(new Rectangle(0, 0, BaseImage.Width, BaseImage.Height), ImageLockMode.ReadWrite, b.PixelFormat);
/* GetBitsPerPixel just does a switch on the PixelFormat and returns the number */
int bitsPerPixel = Image.GetPixelFormatSize(bData.PixelFormat);
/*the size of the image in bytes */
int size = bData.Stride * bData.Height;
/*Allocate buffer for image*/
byte[] OrigImagedata = new byte[size];
byte[] BurnImagedata = new byte[size];
/*This overload copies data of /size/ into /data/ from location specified (/Scan0/)*/
System.Runtime.InteropServices.Marshal.Copy(bData.Scan0, OrigImagedata, 0, size);
System.Runtime.InteropServices.Marshal.Copy(bData.Scan0, BurnImagedata, 0, size);
for (int i = 0; i < size; i += bitsPerPixel / 8)
{
//double magnitude = 1 / 3d * (data[i] + data[i + 1] + data[i + 2]);
if (OrigImagedata[i + 3] == 0) //If the original image is transparent, make the burn image transparent
{
BurnImagedata[i + 3] = 0;//Make it transparent
}
}
/* This override copies the data back into the location specified */
System.Runtime.InteropServices.Marshal.Copy(OrigImagedata, 0, bData.Scan0, OrigImagedata.Length);
System.Runtime.InteropServices.Marshal.Copy(BurnImagedata, 0, burnData.Scan0, OrigImagedata.Length);
b.UnlockBits(bData);
Burn.UnlockBits(burnData);
Graphics.FromImage(b).DrawImage(Burn,new Point(0,0));
return b;
}
public bool AtLocation(Point location)
{
if (location.X >= MyLocation.X && location.X <= MyLocation.X + Size &&