Metafile problem...
-
Hi all! I'm coding a class to print barcodes. One of my methods is this:
public Metafile GetMetafileImage() { PrinterSettings ps = new PrinterSettings(); Graphics g = ps.CreateMeasurementGraphics(); IntPtr pHdc = g.GetHdc(); Metafile mf = new Metafile(pHdc, EmfType.EmfOnly); g.ReleaseHdc(pHdc); g.Dispose(); g = Graphics.FromImage(mf); g.PageUnit = GraphicsUnit.Millimeter; g.PageScale = 1; Brush b = Brushes.White; g.FillRectangle(b, 0, 0, whiteMarginWidth, height); float x = whiteMarginWidth; foreach (char ch in encodedValueString) { if (ch == '1') b = Brushes.Black; else b = Brushes.White; g.FillRectangle(b, x, 0, x + barSize, height); x += barSize; } b = Brushes.White; g.FillRectangle(b, x, 0, x + whiteMarginWidth, height); // <-- this g.Dispose(); return mf; }
It works fine but the last FillRectangle operation. Its width isn't "whiteMarginWidth", but it's larger than "whiteMarginWidth". It seems to fill a rectangle whose wide reach the "end" of a region, perhaps a region set by the printer handle... I can't solve this problem :doh: Can you help me? Thank you! Luca -
Hi all! I'm coding a class to print barcodes. One of my methods is this:
public Metafile GetMetafileImage() { PrinterSettings ps = new PrinterSettings(); Graphics g = ps.CreateMeasurementGraphics(); IntPtr pHdc = g.GetHdc(); Metafile mf = new Metafile(pHdc, EmfType.EmfOnly); g.ReleaseHdc(pHdc); g.Dispose(); g = Graphics.FromImage(mf); g.PageUnit = GraphicsUnit.Millimeter; g.PageScale = 1; Brush b = Brushes.White; g.FillRectangle(b, 0, 0, whiteMarginWidth, height); float x = whiteMarginWidth; foreach (char ch in encodedValueString) { if (ch == '1') b = Brushes.Black; else b = Brushes.White; g.FillRectangle(b, x, 0, x + barSize, height); x += barSize; } b = Brushes.White; g.FillRectangle(b, x, 0, x + whiteMarginWidth, height); // <-- this g.Dispose(); return mf; }
It works fine but the last FillRectangle operation. Its width isn't "whiteMarginWidth", but it's larger than "whiteMarginWidth". It seems to fill a rectangle whose wide reach the "end" of a region, perhaps a region set by the printer handle... I can't solve this problem :doh: Can you help me? Thank you! Luca -
Change "x + whiteMarginWidth" to "whiteMarginWidth". That parameter is the width of the rectangle, NOT the right x-coordinate of the rectangle. -Jeff