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.

297 lines
11 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ZZ_BarcpdeFromFont
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string inp = Bar25I(this.textBox1.Text);
Font f = new Font("Bar 25i c", 46);
BarcodeLib.clsBarI25 barbarcode = new BarcodeLib.clsBarI25();
pictureBox1.Image = barbarcode.GetBarI25(Color.Black, Color.White, inp, "Bar 25i c HR", 36, 350, 50, " U", "Futur Book", 8,270);
//pictureBox1.Image= DrawText(inp, f, Color.Black, 140,textBox1.Text+ " U");
//pictureBox1.Image = DrawText1(Color.Black, Color.White, "Bar 25i c HR", 36, inp, 350, 50, " U",0);
}
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);
}
//private Image DrawText(String text, Font font, Color textColor, Color backColor)
//{
// //first, create a dummy bitmap just to get a graphics object
// Image img = new Bitmap(1, 1);
// Graphics drawing = Graphics.FromImage(img);
// //measure the string to see how big the image needs to be
// SizeF textSize = drawing.MeasureString(text, font);
// //free up the dummy image and old graphics object
// img.Dispose();
// drawing.Dispose();
// //create a new image of the right size
// img = new Bitmap((int)textSize.Width, (int)textSize.Height);
// drawing = Graphics.FromImage(img);
// //paint the background
// drawing.Clear(backColor);
// //create a brush for the text
// Brush textBrush = new SolidBrush(textColor);
// drawing.DrawString(text, font, textBrush, 0, 0);
// drawing.Save();
// textBrush.Dispose();
// drawing.Dispose();
// return img;
//}
public Image DrawText(String text, Font font, Color textColor, int maxWidth, String path)
{
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font, maxWidth);
//set the stringformat flags to rtl
StringFormat sf = new StringFormat();
//uncomment the next line for right to left languages
//sf.FormatFlags = StringFormatFlags.DirectionRightToLeft;
sf.Trimming = StringTrimming.Word;
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
int multi = 4;
//create a new image of the right size
img = new Bitmap((int)textSize.Width*multi, (int)textSize.Height);
drawing = Graphics.FromImage(img);
//Adjust for high quality
drawing.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
drawing.InterpolationMode = InterpolationMode.HighQualityBilinear;
drawing.PixelOffsetMode = PixelOffsetMode.HighQuality;
drawing.SmoothingMode = SmoothingMode.HighQuality;
drawing.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
//paint the background
drawing.Clear(Color.Transparent);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
Font ftext = new Font("Arial", 15,FontStyle.Bold);
drawing.DrawString(text, font, textBrush, new RectangleF(0, 0, textSize.Width*multi, textSize.Height), sf);
drawing.DrawString(path, ftext, textBrush, new RectangleF(50, 50, textSize.Width*multi , textSize.Height ), sf);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
img.Save(path, System.Drawing.Imaging.ImageFormat.Png);
img.Dispose();
}
public Image DrawText1(Color foreColor, Color backColor, string fontName, int fontSize, string txt, int width, int height, string zusatz, int rotation)
{
Bitmap img = new Bitmap(width, height);
Graphics Gimg = Graphics.FromImage(img);
Font imgFont = new Font(fontName, fontSize);
PointF imgPoint = new PointF(5, 5);
SolidBrush bForeColor = new SolidBrush(foreColor);
SolidBrush bBackColor = new SolidBrush(backColor);
Gimg.FillRectangle(bBackColor, 0, 0, width, height);
Gimg.DrawString(txt, imgFont, bForeColor, imgPoint);
Font fzFont = new Font("Arial", 9);
SizeF textSize = Gimg.MeasureString(zusatz, fzFont, 50);
PointF imgPointZusatz= new PointF(width-textSize.Width-15, 30);
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;
}
public static Image RotateImage1(Image img, float rotationAngle)
{
//create an empty Bitmap image
Bitmap bmp = new Bitmap(img.Width, img.Height);
//turn the Bitmap into a Graphics object
Graphics gfx = Graphics.FromImage(bmp);
//now we set the rotation point to the center of our image
gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
//now rotate the image
gfx.RotateTransform(rotationAngle);
gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
//set the InterpolationMode to HighQualityBicubic so to ensure a high
//quality image once it is transformed to the specified size
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
//now draw our new image onto the graphics object
gfx.DrawImage(img, new Point(0, 0));
//dispose of our Graphics object
gfx.Dispose();
//return the image
return bmp;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}