From 95bfbf6cb38cae7a7352d8aabb301cb398987f3b Mon Sep 17 00:00:00 2001 From: Nathan Phillip Brink Date: Sun, 29 Nov 2020 22:59:29 -0500 Subject: [PATCH] Allow specifying DPI for created Bitmap instance. Add new public properties HorizontalResolution and VerticalResolution which is used to set the resolution of the create Bitmap. Fixes #106. --- BarcodeStandard/BarcodeLib.cs | 22 +++++++++++++++++----- BarcodeStandard/Labels.cs | 8 ++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/BarcodeStandard/BarcodeLib.cs b/BarcodeStandard/BarcodeLib.cs index 10dc904..a908da8 100644 --- a/BarcodeStandard/BarcodeLib.cs +++ b/BarcodeStandard/BarcodeLib.cs @@ -81,6 +81,10 @@ public Barcode(string data, TYPE iType) #region Constants /// + /// The default resolution of 96 dots per inch. + /// + const float DefaultResolution = 96f; + /// /// The number of pixels in one point at 96DPI. Since there are 72 points in an inch, this is /// 96/72. /// @@ -89,7 +93,7 @@ public Barcode(string data, TYPE iType) /// pixels to avoid being affected by the system DPI. See issue #100 /// and https://stackoverflow.com/a/10800363. /// - public const float DotsPerPointAt96Dpi = 96f / 72; + public const float DotsPerPointAt96Dpi = DefaultResolution / 72; #endregion #region Properties @@ -190,6 +194,14 @@ public int Height set { _Height = value; } } /// + /// The number of pixels per horizontal inch. Used when creating the Bitmap. + /// + public float HoritontalResolution { get; set; } = DefaultResolution; + /// + /// The number of pixels per vertical inch. Used when creating the Bitmap. + /// + public float VerticalResolution { get; set; } = DefaultResolution; + /// /// If non-null, sets the width of a bar. is ignored and calculated automatically. /// public int? BarWidth { get; set; } @@ -533,10 +545,10 @@ public string GenerateBarcode(string raw_data = "") /// Create and preconfigures a Bitmap for use by the library. Ensures it is independent from /// system DPI, etc. /// - internal static Bitmap CreateBitmap(int width, int height) + internal Bitmap CreateBitmap(int width, int height) { var bitmap = new Bitmap(width, height); - bitmap.SetResolution(96, 96); + bitmap.SetResolution(HoritontalResolution, VerticalResolution); return bitmap; } /// @@ -661,7 +673,7 @@ private Bitmap Generate_Image() string defTxt = RawData; string labTxt = defTxt.Substring(0, 1) + "--" + defTxt.Substring(1, 6) + "--" + defTxt.Substring(7); - Font labFont = new Font(this.LabelFont != null ? this.LabelFont.FontFamily.Name : "Arial", Labels.getFontsize(Width, Height, labTxt) * DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel); + Font labFont = new Font(this.LabelFont != null ? this.LabelFont.FontFamily.Name : "Arial", Labels.getFontsize(this, Width, Height, labTxt) * DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel); if (this.LabelFont != null) { this.LabelFont.Dispose(); @@ -764,7 +776,7 @@ private Bitmap Generate_Image() string labTxt = defTxt.Substring(0, 1) + "--" + defTxt.Substring(1, 6) + "--" + defTxt.Substring(7); Font font = this.LabelFont; - Font labFont = new Font(font != null ? font.FontFamily.Name : "Arial", Labels.getFontsize(Width, Height, labTxt) * DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel); + Font labFont = new Font(font != null ? font.FontFamily.Name : "Arial", Labels.getFontsize(this, Width, Height, labTxt) * DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel); if (font != null) { diff --git a/BarcodeStandard/Labels.cs b/BarcodeStandard/Labels.cs index 4ec1ff4..5414d9f 100644 --- a/BarcodeStandard/Labels.cs +++ b/BarcodeStandard/Labels.cs @@ -154,7 +154,7 @@ public static Image Label_EAN13(Barcode Barcode, Bitmap img) int iBarWidth = Barcode.Width / Barcode.EncodedValue.Length; string defTxt = Barcode.RawData; - using (Font labFont = new Font("Arial", getFontsize(Barcode.Width - Barcode.Width % Barcode.EncodedValue.Length, img.Height, defTxt) * Barcode.DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel)) + using (Font labFont = new Font("Arial", getFontsize(Barcode, Barcode.Width - Barcode.Width % Barcode.EncodedValue.Length, img.Height, defTxt) * Barcode.DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel)) { int shiftAdjustment; switch (Barcode.Alignment) @@ -243,7 +243,7 @@ public static Image Label_UPCA(Barcode Barcode, Bitmap img) int halfBarWidth = (int)(iBarWidth * 0.5); string defTxt = Barcode.RawData; - using (Font labFont = new Font("Arial", getFontsize((int)((Barcode.Width - Barcode.Width % Barcode.EncodedValue.Length) * 0.9f), img.Height, defTxt) * Barcode.DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel)) + using (Font labFont = new Font("Arial", getFontsize(Barcode, (int)((Barcode.Width - Barcode.Width % Barcode.EncodedValue.Length) * 0.9f), img.Height, defTxt) * Barcode.DotsPerPointAt96Dpi, FontStyle.Regular, GraphicsUnit.Pixel)) { int shiftAdjustment; switch (Barcode.Alignment) @@ -318,14 +318,14 @@ public static Image Label_UPCA(Barcode Barcode, Bitmap img) }//catch }//Label_UPCA - public static int getFontsize(int wid, int hgt, string lbl) + public static int getFontsize(Barcode barcode, int wid, int hgt, string lbl) { //Returns the optimal font size for the specified dimensions int fontSize = 10; if (lbl.Length > 0) { - Image fakeImage = Barcode.CreateBitmap(1, 1); //As we cannot use CreateGraphics() in a class library, so the fake image is used to load the Graphics. + Image fakeImage = barcode.CreateBitmap(1, 1); //As we cannot use CreateGraphics() in a class library, so the fake image is used to load the Graphics. // Make a Graphics object to measure the text. using (Graphics gr = Graphics.FromImage(fakeImage))