private void UpdatePreview() { int scaleX = this.Width / this.mPointsWidth; int scaleY = this.Height / this.mPointsHeight; this.mScale = Math.Min(scaleX, scaleY); if (this.mScale == 0) { this.mScale = 1; } int previewWidth = this.mPointsWidth * this.mScale; int previewHeight = this.mPointsHeight * this.mScale; if (this.mBmpPreview.Width != previewWidth || this.mBmpPreview.Height != previewHeight) { //if (this.mBmpPreview != null) // this.mBmpPreview.Dispose(); //if (this.mBmpData != null) // this.mBmpData = null; int stride = Convert.ToInt32(Math.Ceiling((float)previewWidth / 8.0f)); this.mBmpPreview = new Bitmap(previewWidth, previewHeight, PixelFormat.Format1bppIndexed); } Rectangle rectSource = Rectangle.FromLTRB(0, 0, this.mPointsWidth - 1, this.mPointsHeight - 1); BitmapData bmdSource = this.mBmp.LockBits(rectSource, ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed); BitmapData bmdDestination = this.mBmpPreview.LockBits(Rectangle.FromLTRB(0, 0, previewWidth, previewHeight), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed); for (int x = 0; x < this.mPointsWidth; x++) { //System.Diagnostics.Debug.WriteLine(" "); for (int y = 0; y < this.mPointsHeight; y++) { bool value = BitmapHelper.GetPixel(bmdSource, x, y); //System.Diagnostics.Debug.Write(String.Format("{0}", value ? 1 : 0)); int destX = x * this.mScale; int destY = y * this.mScale; for (int x2 = 0; x2 < this.mScale; x2++) { for (int y2 = 0; y2 < this.mScale; y2++) { if (destX + x2 >= previewWidth || destY + y2 >= previewHeight) { } else { BitmapHelper.SetPixel(bmdDestination, destX + x2, destY + y2, value); } } } } } this.mBmp.UnlockBits(bmdSource); this.mBmpPreview.UnlockBits(bmdDestination); }
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); }