Can I use zxing to generate a code_128 barcode image? - c#

I've been fiddling around, but can't seem to find a good documentation for it.
Can I make ZXing generate a CODE_128 barcode image? How?

If you are using ZXing.Net you can do it with the following snippet:
var content = "123456789012345678";
var writer = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128
};
var bitmap = writer.Write(content);

Based on some research this is a possible implementation in MVC:
using (MemoryStream memoryStream = new MemoryStream())
{
var writer = new BarcodeWriter
{
Format = BarcodeFormat.CODE_128
};
Bitmap barcodeBitmap;
var barcode = writer.Write(scan);
MemoryStream stream = new MemoryStream();
barcode.Save(memoryStream,ImageFormat.Png);
ViewBag.OneD = "data:image/png;base64," + Convert.ToBase64String(memoryStream.ToArray());
}

Related

How to produce a System.Drawing.Image from SVG on .net core?

I have found many libraries to read SVG and transform it to System.Drawing.Image or png in C# framework, but I cannot find any way to do it in .net core.
And if I use Image.FromFile, I get an OutOfMemoryException (supposedly because SVG is not a rasterized format).
Any tips on how to use Image to read SVG or any open source library that works in .net core?
Skiasharp by Xamarin team seems to be a good choice. There's already a document of API on learn.microsoft.com. For more detailed information, see Mono/SkiaSharp and Mono/mono/SkiaSharp.Extended
You can install the offical svg extension on nuget by dotnet add package SkiaSharp.Svg:
<PackageReference Include="SkiaSharp.Svg" Version="1.60.0" />
Demo:
var svgSrc=Path.Combine(Directory.GetCurrentDirectory(),"img.svg");
string svgSaveAs = "xyz.png";
var quality = 100;
var svg = new SkiaSharp.Extended.Svg.SKSvg();
var pict = svg.Load(svgSrc);
var dimen = new SkiaSharp.SKSizeI(
(int) Math.Ceiling(pict.CullRect.Width),
(int) Math.Ceiling(pict.CullRect.Height)
);
var matrix = SKMatrix.MakeScale(1,1);
var img = SKImage.FromPicture(pict,dimen,matrix);
// convert to PNG
var skdata = img.Encode(SkiaSharp.SKEncodedImageFormat.Png,quality);
using(var stream = File.OpenWrite(svgSaveAs)){
skdata.SaveTo(stream);
}
Screenshot:
You can use ImageMagick to convert svg to any format.
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="7.14.0" />
Below method converts svg base64 string to other formats.
public static string Base64ToImageStream(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
using (var msOut = new MemoryStream())
{
MagickReadSettings readSettings = new MagickReadSettings()
{
Format = MagickFormat.Svg,
Width = 60,
Height = 40,
BackgroundColor = MagickColors.Transparent
};
using (MagickImage image = new MagickImage(imageBytes, readSettings))
{
image.Format = MagickFormat.Png; // Specify the format you need
image.Write(msOut);
byte[] data = image.ToByteArray();
return Convert.ToBase64String(data);
// In case if you want the output in stream
// byte[] imgByte = Convert.FromBase64String(pngBase64);
// var pngStream = new MemoryStream(imgByte, 0, imgByte.Length);
// return pngStream;
}
}
}
}

How to convert DocX from Xceed.Words.NET library to pdf and save it in a memory stream

I want to convert word byte array to pdf byte array.
I am using Xceed.Words.NET library
var stream = new MemoryStream(sourceFile.AttachedFile);
var doc = DocX.Load(stream);
var ms = new MemoryStream();
doc.SaveAs(ms);
var wByteArray = ms.GetBuffer();
Use this:
var stream = new MemoryStream(sourceFile.AttachedFile);
using (var document = DocX.Load(stream))
{
stream = new MemoryStream();
DocX.ConvertToPdf(document, stream);
}
var bytes = stream.ToArray();
As mentioned in the comment, you need a professional version of DocX library to convert a Word document to PDF.
If you're looking for free solution then perhaps you could try out GemBox.Document, its free version does support converting to PDF, but it has a document size limitation.
You can use it like this:
ComponentInfo.SetLicense("FREE-LIMITED-KEY");
var stream = new MemoryStream(sourceFile.AttachedFile);
var document = DocumentModel.Load(stream, LoadOptions.DocxDefault);
stream = new MemoryStream();
document.Save(stream, SaveOptions.PdfDefault);
var bytes = stream.ToArray();

how to get header text with image from .DOCX file

i want to get header image form doc file. i use following code it gives me image path but i can't get it
DocumentFormat.OpenXml.Packaging.ImagePart img = header.ImageParts.FirstOrDefault();
string imgpath = img.Uri.OriginalString;
I think your approach didn't work because the doc file is a zip file.
I don't know in which format you need that image, but you can try something like this to retrieve an image object. I updated my answer with an working example hope it helps.
using (var document = WordprocessingDocument.Open("your document path", true))
{
//Get the header
var header = document.MainDocumentPart.HeaderParts.First();
//These are your paragraphs where you can get the headers Text from
var paragraphList = header.Header.Descendants<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
//Get the imageId
string imgId = header.GetIdOfPart(header.ImageParts.First());
var imageSource=new BitmapImage();
//Get the imageStream
using (var imgStream = ((ImagePart)header.GetPartById(imgId)).GetStream())
{
//Copy stream to BitmapImage
using (var memoryStream = new MemoryStream())
{
imgStream.CopyTo(memoryStream);
memoryStream.Position = 0;
imageSource.BeginInit();
imageSource.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
imageSource.CacheOption = BitmapCacheOption.OnLoad;
imageSource.UriSource = null;
imageSource.StreamSource = memoryStream;
imageSource.EndInit();
}
imageSource.Freeze();
//Save BitmapImage to file
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(imageSource));
using (var stream = new FileStream("your path for the image.png", FileMode.Create))
encoder.Save(stream);
}
}
and this is an example how you can get position of your picture, but keep in mind it will only work if your picture got an absolute position.
List<DocumentFormat.OpenXml.Wordprocessing.Drawing> sdtElementDrawing =
header.Header.Descendants<DocumentFormat.OpenXml.Wordprocessing.Drawing>().ToList();
var distL= sdtElementDrawing.First().Anchor.DistanceFromLeft;

Decode QR code from Byte[] using ZXing in C#

I can decode QR code from an image file as follows-
Bitmap bitmap = new Bitmap(imagePath);
BarcodeReader reader = new BarcodeReader();
Result result = reader.Decode(bitmap);
decodedData = result.Text;
But I want it to do from Byte[].
Byte[] imagefile;
using (var binaryReader = new BinaryReader(Request.Files["files"].InputStream))
{
imagefile = binaryReader.ReadBytes(Request.Files["files"].ContentLength);//image
}
I would like to read QR code from this imagefile variable. Is there any way to do it?
Thank you.
How about:
using (var binaryReader = new BinaryReader(Request.Files["files"].InputStream))
{
byte[] imagefile = binaryReader.ReadBytes(Request.Files["files"].ContentLength); //image
using (MemoryStream memory = new MemoryStream(imagefile))
using (Image bitmap = Image.FromStream(memory)
{
BarcodeReader reader = new BarcodeReader();
Result result = reader.Decode(bitmap);
decodedData = result.Text;
}
}
Or may be even shorter:
using (Image bitmap = Image.FromStream(Request.Files["files"].InputStream))
{
BarcodeReader reader = new BarcodeReader();
Result result = reader.Decode(bitmap);
decodedData = result.Text;
}

ZXing.Net Encode string to QR Code in CF

How could I encode my string into a QR Code using ZXing.Net?
I can already decode, but having problems in encoding. It has an error that says: no encoder available for format AZTEC.
Here is my code:
IBarcodeWriter writer = new BarcodeWriter();
Bitmap barcodeBitmap;
var result = writer.Encode("Hello").ToBitmap();
barcodeBitmap = new Bitmap(result);
pictureBox1.Image = barcodeBitmap;
You don't fully initialize the BarcodeWriter. You have to set the barcode format.
Try the following code snippet:
IBarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE };
var result = writer.Write("Hello");
var barcodeBitmap = new Bitmap(result);
pictureBox1.Image = barcodeBitmap;
#dizzytri99er
Seems that I have sucessfully encoded a message with ZXing.net therefore I think it does support Aztec encoding
This is the code I have used;
static void Main(string[] args)
{
IBarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.AZTEC
};
Bitmap aztecBitmap;
var result = writer.Write("I love you ;)");
aztecBitmap = new Bitmap(result);
using (var stream = new FileStream("test.bmp", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var aztecAsBytes = ImageToByte(aztecBitmap);
stream.Write(aztecAsBytes, 0, aztecAsBytes.Length);
}
}
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
could it possibly be the size of the codes your are scanning?
take a look here
best way to generate and encode QR codes would be...
QR code encoder and Zbar

Categories

Resources