public BitmapEditorControl()
        {
            //InitializeComponent();
            this.SuspendLayout();
            this.BackColor = System.Drawing.Color.Transparent;
            this.Name      = "BitmapEditorControl";
            this.Size      = new System.Drawing.Size(200, 160);
            this.ResumeLayout(false);

            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.mGridPen = new Pen(Color.Silver);
            //this.mSelPen = new Pen(Color.Gray);
            this.mPointsHeight = 10;
            this.mPointsWidth  = 10;
            this.mMouseOverX   = 0;
            this.mMouseOverY   = 0;

            this.mBrightnessEdge = 0.5f;

            this.mBmp = new Bitmap(this.mPointsWidth, this.mPointsHeight, PixelFormat.Format1bppIndexed);

            bool       def = SavedContainer <Options> .Instance.SetBitsByDefault;
            BitmapData bmd = this.mBmp.LockBits(new Rectangle(0, 0, this.mPointsWidth, this.mPointsHeight), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);

            for (int x = 0; x < this.mPointsWidth; x++)
            {
                for (int y = 0; y < this.mPointsHeight; y++)
                {
                    BitmapHelper.SetPixel(bmd, x, y, def);
                }
            }
            this.mBmp.UnlockBits(bmd);

            //this.SetPixel(0, 0, true);
            //this.SetPixel(this.mPointsWidth - 1, 0, true);
            //this.SetPixel(0, this.mPointsHeight - 1, true);
            //this.SetPixel(this.mPointsWidth - 1, this.mPointsHeight - 1, true);

            this.mBmpPreview = new Bitmap(this.mPointsWidth, this.mPointsHeight);
            this.mScale      = 1;
        }
        public static Bitmap LoadFromXml(XmlNode node)
        {
            int    width  = Convert.ToInt32(node.Attributes["width"].Value, CultureInfo.InvariantCulture);
            int    height = Convert.ToInt32(node.Attributes["height"].Value, CultureInfo.InvariantCulture);
            Bitmap bmp    = new Bitmap(width, height, PixelFormat.Format1bppIndexed);

            BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);

            for (int y = 0; y < height; y++)
            {
                string  ypath    = String.Format(CultureInfo.InvariantCulture, "line[@index={0}]", y);
                XmlNode nodeLine = node.SelectSingleNode(ypath);
                if (nodeLine == null)
                {
                    throw new Exception("Line node not found: " + ypath);
                }
                for (int x = 0; x < width; x++)
                {
                    int     col        = Convert.ToInt32(x / 8);
                    int     subx       = x % 8;
                    string  xpath      = String.Format(CultureInfo.InvariantCulture, "column[@index={0}]", col);
                    XmlNode nodeColumn = nodeLine.SelectSingleNode(xpath);
                    if (nodeColumn == null)
                    {
                        throw new Exception("Column node not found: " + xpath);
                    }
                    string byteData = nodeColumn.InnerText;
                    if (byteData[subx] == '0')
                    {
                        //bmp.SetPixel(x, y, Color.White);
                        BitmapHelper.SetPixel(bmd, x, y, false);
                    }
                    else
                    {
                        //bmp.SetPixel(x, y, Color.Black);
                        BitmapHelper.SetPixel(bmd, x, y, true);
                    }
                }
            }
            bmp.UnlockBits(bmd);
            return(bmp);
        }
        private bool LoadBitmapFromXml(string filename)
        {
            bool result = false;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filename);
                XmlNode root = doc.DocumentElement;
                if (root.Attributes["type"] != null)
                {
                    if (root.Attributes["type"].Value == "image")
                    {
                        XmlNode nodeBitmap = root.SelectSingleNode("bitmap");
                        if (nodeBitmap != null)
                        {
                            this.mEditor.BmpEditor.Bmp = BitmapHelper.LoadFromXml(nodeBitmap);
                            result = true;
                        }
                        else
                        {
                            throw new Exception("Invalid format of file, 'bitmap' node not found");
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid format of file, attribute 'type' must be equal to 'image'");
                    }
                }
                else
                {
                    throw new Exception("Invalid format of file, attribute 'type' not defined");
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error while loading file", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            return(result);
        }
        public static Bitmap GetCharacterBitmap(char c, Font font, FontWidthMode widthMode, int width, float edge)
        {
            bool setByDef = SavedContainer <Options> .Instance.SetBitsByDefault;

            Size   sz = TextRenderer.MeasureText(new string(c, 1), font);
            Bitmap bmp;

            if (widthMode == FontWidthMode.Monospaced)
            {
                bmp = new Bitmap(width, sz.Height);
            }
            else
            {
                bmp = new Bitmap(sz.Width, sz.Height);
            }
            Graphics gr = Graphics.FromImage(bmp);

            gr.FillRectangle((setByDef ? Brushes.White : Brushes.Black), 0, 0, bmp.Width, bmp.Height);
            gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
            gr.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.None;
            gr.DrawString(new string(c, 1), font, (setByDef ? Brushes.Black : Brushes.White), 0, 0);
            bmp = GetMonochrome(bmp, edge);
            if (widthMode == FontWidthMode.Proportional)
            {
                Rectangle shrink = BitmapHelper.CalcShrink(bmp);
                int       left   = -shrink.X;
                int       top    = -shrink.Y;
                int       right  = -(bmp.Width - shrink.Width - shrink.X);
                int       bottom = -(bmp.Height - shrink.Height - shrink.Y);

                left++;
                right++;
                bmp = BitmapHelper.Resize(bmp, left, 0, right, 0);
            }
            return(bmp);
        }
 public override void Inverse()
 {
     this.mEditor.BmpEditor.Bmp = BitmapHelper.Inverse(this.mEditor.BmpEditor.Bmp);
     this.mEditor.BmpEditor.Invalidate();
 }
        public static void SaveToXml(Bitmap sourceBitmap, XmlNode node, XmlSavingOptions options)
        {
            Bitmap bmp = BitmapHelper.RotateFlip(sourceBitmap, options.FlipHorizontal, options.FlipVertical, options.Angle);

            if (options.Inverse)
            {
                bmp = BitmapHelper.Inverse(bmp);
            }
            if ((bmp.Width % 8) != 0)
            {
                if (options.AlignRight)
                {
                    bmp = BitmapHelper.Resize(bmp, 8 - bmp.Width % 8, 0, 0, 0);
                }
            }
            //separate node for bitmap's data
            //XmlNode nodeBitmap = node.AppendChild(node.OwnerDocument.CreateElement("bitmap"));
            XmlNode nodeBitmap = node;

            //bitmap info
            (nodeBitmap as XmlElement).SetAttribute("width", Convert.ToString(sourceBitmap.Width, CultureInfo.InvariantCulture));
            (nodeBitmap as XmlElement).SetAttribute("height", Convert.ToString(sourceBitmap.Height, CultureInfo.InvariantCulture));
            //preview node, all bits at one line
            XmlNode nodePreview = nodeBitmap.AppendChild(node.OwnerDocument.CreateElement("preview"));

            BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed);

            for (int y = 0; y < bmp.Height; y++)
            {
                //bitmap's line
                XmlNode nodeLine = nodeBitmap.AppendChild(node.OwnerDocument.CreateElement("line"));
                (nodeLine as XmlElement).SetAttribute("index", Convert.ToString(y, CultureInfo.InvariantCulture));

                StringBuilder byteData = new StringBuilder();
                StringBuilder lineData = new StringBuilder();
                for (int x = 0; x < bmp.Width; x++)
                {
                    //if (bmp.GetPixel(x, y).GetBrightness() > this.mBrightnessEdge)
                    if (BitmapHelper.GetPixel(bmd, x, y))
                    {
                        byteData.Append("1");
                    }
                    else
                    {
                        byteData.Append("0");
                    }
                    //if byte filled or end of line
                    if ((x % 8) == 7 || x == (bmp.Width - 1))
                    {
                        XmlNode nodeColumn = nodeLine.AppendChild(node.OwnerDocument.CreateElement("column"));
                        (nodeColumn as XmlElement).SetAttribute("index", Convert.ToString((int)(x / 8), CultureInfo.InvariantCulture));

                        //ensure what 8 bits (1 byte)
                        while (byteData.Length < 8)
                        {
                            byteData.Append("0");
                        }

                        nodeColumn.InnerText = byteData.ToString();
                        lineData.Append(byteData);
                        if (options.MirrorEachByte)
                        {
                            string str = byteData.ToString();
                            byteData.Length = 0;
                            foreach (char c in str)
                            {
                                byteData.Insert(0, c);
                            }
                            nodeColumn.InnerText = byteData.ToString();
                        }
                        byteData.Length = 0;
                    }
                }
                lineData = lineData.Replace('0', '_').Replace('1', '#');
                XmlNode nodePreviewLine = nodePreview.AppendChild(node.OwnerDocument.CreateElement("line"));
                nodePreviewLine.InnerText = lineData.ToString();
            }
            bmp.UnlockBits(bmd);
        }
 public override void Inverse()
 {
     this.mFontEdCtrl.ImageEditor.BmpEditor.Bmp = BitmapHelper.Inverse(this.mFontEdCtrl.ImageEditor.BmpEditor.Bmp);
 }
        private bool LoadFontFromXml(string filename)
        {
            bool result = false;

            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(filename);
                XmlNode root = doc.DocumentElement;
                if (root.Attributes["type"] != null)
                {
                    if (root.Attributes["type"].Value == "font")
                    {
                        XmlNodeList nodesChar = root.SelectNodes("chars/char");
                        if (nodesChar != null && nodesChar.Count > 0)
                        {
                            List <byte> bytes = new List <byte>();
                            this.mFontEdCtrl.FontContainer.CharBitmaps.Clear();
                            foreach (XmlNode nodeChar in nodesChar)
                            {
                                XmlNodeList nodeEncodingBytes = nodeChar.SelectNodes("encoding[@codepage=65001]/bytes/byte");
                                bytes.Clear();
                                foreach (XmlNode nodeByte in nodeEncodingBytes)
                                {
                                    bytes.Add(Convert.ToByte(nodeByte.InnerText, 16));
                                }
                                char c = Encoding.UTF8.GetString(bytes.ToArray())[0];

                                XmlNode nodeBitmap = nodeChar["bitmap"];
                                Bitmap  charBitmap = BitmapHelper.LoadFromXml(nodeBitmap);
                                this.mFontEdCtrl.FontContainer.CharBitmaps.Add(c, charBitmap);
                            }

                            if (root["family"] != null)
                            {
                                this.mFontEdCtrl.FontContainer.FontFamily = new FontFamily(root["family"].InnerText);
                            }
                            if (root["size"] != null)
                            {
                                this.mFontEdCtrl.FontContainer.Size = Convert.ToInt32(root["size"].InnerText, CultureInfo.InvariantCulture);
                            }
                            if (root["style"] != null)
                            {
                                this.mFontEdCtrl.FontContainer.Style = (FontStyle)Enum.Parse(typeof(FontStyle), root["style"].InnerText);
                            }

                            this.mFontEdCtrl.ApplyContainer();
                            result = true;
                        }
                        else
                        {
                            throw new Exception("Invalid format of file, 'char' nodes not found");
                        }
                    }
                    else
                    {
                        throw new Exception("Invalid format of file, attribute 'type' must be equal to 'font'");
                    }
                }
                else
                {
                    throw new Exception("Invalid format of file, attribute 'type' not defined");
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error while loading file", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            return(result);
        }