private void OnClick(object sender, EventArgs e) { if (sender == this.bShrink) { Rectangle rect = BitmapHelper.CalcShrink(this.BmpEditor.Bmp); this.numLeft.Value = -rect.X; this.numTop.Value = -rect.Y; this.numRight.Value = -(this.BmpEditor.Bmp.Width - rect.Width - rect.X); this.numBottom.Value = -(this.BmpEditor.Bmp.Height - rect.Height - rect.Y); } if (sender == this.bApplyResize) { int left = Convert.ToInt32(this.numLeft.Value); int top = Convert.ToInt32(this.numTop.Value); int right = Convert.ToInt32(this.numRight.Value); int bottom = Convert.ToInt32(this.numBottom.Value); this.BmpEditor.Bmp = BitmapHelper.Resize(this.BmpEditor.Bmp, left, top, right, bottom); } if (sender == this.bImport) { using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.CheckFileExists = true; ofd.CheckPathExists = true; ofd.DefaultExt = ".*"; ofd.Filter = "Windows Bitmap (*.bmp)|*.bmp"; ofd.Title = "Open image file"; if (ofd.ShowDialog() == DialogResult.OK) { string filename = ofd.FileName; Bitmap bmp = new Bitmap(filename); using (FormColor2BW formC2BW = new FormColor2BW()) { formC2BW.ImageOriginal = bmp; if (formC2BW.ShowDialog() == DialogResult.OK) { this.BmpEditor.Bmp = formC2BW.ImageResult; } } } } } if (sender == this.bExport) { using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.AddExtension = true; sfd.CheckPathExists = true; sfd.DefaultExt = ".*"; sfd.Filter = "Windows Bitmap (*.bmp)|*.bmp"; sfd.OverwritePrompt = true; sfd.Title = "Save image file..."; if (sfd.ShowDialog() == DialogResult.OK) { this.BmpEditor.Bmp.Save(sfd.FileName); } } } }
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 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); }