public static string GetStringFromImage(Image image)
{
if (image != null) {
ImageConverter ic = new ImageConverter();
byte[] buffer = (byte[])ic.ConvertTo(image, typeof(byte[]));
return Convert.ToBase64String(buffer, Base64FormattingOptions.InsertLineBreaks);
} else {
return null;
}
}
public static Image GetImageFromString(string base64String)
{
if (string.IsNullOrWhiteSpace(base64String))
return null;
byte[] buffer = Convert.FromBase64String(base64String);
if (buffer != null) {
ImageConverter ic = new ImageConverter();
return ic.ConvertFrom(buffer) as Image;
} else {
return null;
}
}
private void chekform(bool chek)
{
if (chek) {
Background = My.Resources.mini;
minichek = chek;
} else if (chek == false) {
Background = My.Resources.main;
minichek = chek;
} else if (Background == null) {
minichek = false;
imageform = GetStringFromImage(My.Resources.main);
Background = GetImageFromString(imageform);
}
this.BackgroundImage = Background;
this.BackgroundImageLayout = ImageLayout.None;
this.Region = MakeRegion(Background);
this.Size = new Size(Background.Width, Background.Height);
}
private System.Drawing.Region MakeRegion(Bitmap skinBmp)
{
if (skinBmp == null)
return null;
Region fullRegion = null;
Region lineRegion = new Region();
Color transparentColor;
bool inLine = false;
int startLineX = 0;
transparentColor = skinBmp.GetPixel(0, 0);
for (int y = 0; y <= skinBmp.Height - 1; y++)
{
for (int x = 0; x <= skinBmp.Width - 1; x++)
{
if (skinBmp.GetPixel(x, y).ToArgb == transparentColor.ToArgb)
{
if (inLine)
{
inLine = false;
lineRegion = new Region(new Rectangle(startLineX, y, x - startLineX, 1));
if (fullRegion == null)
{
fullRegion = lineRegion;
}
else
{
fullRegion.Union(lineRegion);
}
}
}
else
{
if (!inLine)
{
inLine = true;
startLineX = x;
}
}
}
}
return fullRegion;
}