Skip to content

Commit

Permalink
Allow specifying DPI for created Bitmap instance.
Browse files Browse the repository at this point in the history
Add new public properties HorizontalResolution and VerticalResolution
which is used to set the resolution of the create Bitmap.

Fixes #106.
  • Loading branch information
binki committed Nov 30, 2020
1 parent ba17ed6 commit 95bfbf6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
22 changes: 17 additions & 5 deletions BarcodeStandard/BarcodeLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public Barcode(string data, TYPE iType)

#region Constants
/// <summary>
/// The default resolution of 96 dots per inch.
/// </summary>
const float DefaultResolution = 96f;
/// <summary>
/// The number of pixels in one point at 96DPI. Since there are 72 points in an inch, this is
/// 96/72.
/// </summary>
Expand All @@ -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.
/// </para></remarks>
public const float DotsPerPointAt96Dpi = 96f / 72;
public const float DotsPerPointAt96Dpi = DefaultResolution / 72;
#endregion

#region Properties
Expand Down Expand Up @@ -190,6 +194,14 @@ public int Height
set { _Height = value; }
}
/// <summary>
/// The number of pixels per horizontal inch. Used when creating the Bitmap.
/// </summary>
public float HoritontalResolution { get; set; } = DefaultResolution;
/// <summary>
/// The number of pixels per vertical inch. Used when creating the Bitmap.
/// </summary>
public float VerticalResolution { get; set; } = DefaultResolution;
/// <summary>
/// If non-null, sets the width of a bar. <see cref="Width"/> is ignored and calculated automatically.
/// </summary>
public int? BarWidth { get; set; }
Expand Down Expand Up @@ -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.
/// </summary>
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;
}
/// <summary>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down
8 changes: 4 additions & 4 deletions BarcodeStandard/Labels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 95bfbf6

Please sign in to comment.