You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
6.8 KiB

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace BarcodeLib
{
public class clsBarI25
{
public Image GetBarI25(Color forecolor, Color backcolor, string text, string barcodefontname, int barcodefontsize, int imagewidth, int imageheight, string zusatztext, string zusatzfontname, int zusatfontsize, int rotation)
{
return DrawText1(forecolor, backcolor,barcodefontname,barcodefontsize,text, imagewidth,imageheight,zusatztext,zusatzfontname,zusatfontsize,rotation);
}
public string Bar25I(string BarTextIn)
{
string Bar25IRet = default;
string BarTextOut = "";
string TempString = "";
long CharValue = 0;
string barcodeout = "";
// Initialize input and output strings
BarTextOut = "";
BarTextIn = BarTextIn.Trim();
// Throw away non-numeric data
TempString = "";
for (int II = 1, loopTo = BarTextIn.Length; II <= loopTo; II++)
{
if (IsNumeric(BarTextIn.Substring(II - 1, 1)))
{
TempString = TempString + BarTextIn.Substring(II - 1, 1);
}
}
// If not an even number of digits, add a leading 0
if (TempString.Length % 2 == 1)
{
TempString = "0" + TempString;
}
// Break digit pairs up and convert to characters- build output string
for (int II = 1, loopTo1 = TempString.Length; II <= loopTo1; II += 2)
{
// Break string into pairs of digits and get value
CharValue = Convert.ToInt32(TempString.Substring(II - 1, 2));
// translate value to ASCII and save in BarTextOut
if (CharValue < 90)
{
BarTextOut = BarTextOut + (char)(CharValue + 33);
}
else
{
BarTextOut = BarTextOut + (char)(CharValue + 71);
}
}
// Build ouput string, trailing space for Windows rasterization bug
barcodeout = "{" + BarTextOut + "} ";
// Return the string
Bar25IRet = barcodeout;
return Bar25IRet;
}
public bool IsNumeric(string value)
{
return value.All(char.IsNumber);
}
public Image DrawText1(Color foreColor, Color backColor, string fontName, int fontSize, string txt, int width, int height, string zusatz,string zusatzfontname, int zusatzfontsize , int rotation)
{
//width = 320;
//height = 50;
string barcodetext = txt;
barcodetext = Bar25I(txt);
Bitmap img = new Bitmap(width, height);
Graphics Gimg = Graphics.FromImage(img);
Font imgFont = new Font(fontName, fontSize);
PointF imgPoint = new PointF(0, 0);
SolidBrush bForeColor = new SolidBrush(foreColor);
SolidBrush bBackColor = new SolidBrush(backColor);
Gimg.FillRectangle(bBackColor, 0, 0, width, height);
Gimg.DrawString(barcodetext, imgFont, bForeColor, imgPoint);
Font fzFont = new Font(zusatzfontname, zusatzfontsize);
SizeF textSize = Gimg.MeasureString(zusatz, fzFont,30);
PointF imgPointZusatz = new PointF(width - textSize.Width , 25);
Gimg.DrawString(zusatz, fzFont, bForeColor, imgPointZusatz);
return RotateImage(img, rotation, true, true, Color.White);
//img.Save(imagePath, ImageFormat.Jpeg);
}
public static Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsizeOk,
bool clipOk, Color backgroundColor)
{
// Test for zero rotation and return a clone of the input image
if (angleDegrees == 0f)
return (Bitmap)inputImage.Clone();
// Set up old and new image dimensions, assuming upsizing not wanted and clipping OK
int oldWidth = inputImage.Width;
int oldHeight = inputImage.Height;
int newWidth = oldWidth;
int newHeight = oldHeight;
float scaleFactor = 1f;
// If upsizing wanted or clipping not OK calculate the size of the resulting bitmap
if (upsizeOk || !clipOk)
{
double angleRadians = angleDegrees * Math.PI / 180d;
double cos = Math.Abs(Math.Cos(angleRadians));
double sin = Math.Abs(Math.Sin(angleRadians));
newWidth = (int)Math.Round(oldWidth * cos + oldHeight * sin);
newHeight = (int)Math.Round(oldWidth * sin + oldHeight * cos);
}
// If upsizing not wanted and clipping not OK need a scaling factor
if (!upsizeOk && !clipOk)
{
scaleFactor = Math.Min((float)oldWidth / newWidth, (float)oldHeight / newHeight);
newWidth = oldWidth;
newHeight = oldHeight;
}
// Create the new bitmap object. If background color is transparent it must be 32-bit,
// otherwise 24-bit is good enough.
Bitmap newBitmap = new Bitmap(newWidth, newHeight, backgroundColor == Color.Transparent ?
PixelFormat.Format32bppArgb : PixelFormat.Format24bppRgb);
newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution);
// Create the Graphics object that does the work
using (Graphics graphicsObject = Graphics.FromImage(newBitmap))
{
graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphicsObject.SmoothingMode = SmoothingMode.HighQuality;
// Fill in the specified background color if necessary
if (backgroundColor != Color.Transparent)
graphicsObject.Clear(backgroundColor);
// Set up the built-in transformation matrix to do the rotation and maybe scaling
graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f);
if (scaleFactor != 1f)
graphicsObject.ScaleTransform(scaleFactor, scaleFactor);
graphicsObject.RotateTransform(angleDegrees);
graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);
// Draw the result
graphicsObject.DrawImage(inputImage, 0, 0);
}
return newBitmap;
}
}
}