Image.Save to MemoryStream fails due to size? - c#

I'm using PDFiumSharp to convert a PDF file to a single image, using the following code:
using System;
using System.IO;
using System.Linq;
using PDFiumSharp;
using System.Drawing.Imaging;
using System.Drawing;
using PDFiumSharp.Types;
public class ImageFromPDF
{
private readonly int DPI = 200;
private readonly int PPI = 72;
private int PointsToPixels(double points)
{
return (int)Math.Ceiling(points / PPI * DPI);
}
public MemoryStream Convert(MemoryStream stream)
{
var outputStream = new MemoryStream();
using (var doc = new PdfDocument(stream.ToArray()))
{
var totalHeight = PointsToPixels(doc.Pages.Sum(bm => bm.Height));
var maxWidth = PointsToPixels(doc.Pages.Max(bm => bm.Width));
using (var img = new Bitmap(maxWidth, totalHeight, PixelFormat.Format32bppArgb))
{
img.SetResolution(DPI, DPI);
using (var g = Graphics.FromImage(img))
{
var heightOffset = 0;
foreach (var page in doc.Pages)
{
using (var bitmap = new PDFiumBitmap(PointsToPixels(page.Width), PointsToPixels(page.Height), false))
{
bitmap.Fill(new FPDF_COLOR(255, 255, 255, 255));
page.Render(bitmap);
using (var bmStream = bitmap.AsBmpStream(DPI, DPI))
{
var widthOffset = (maxWidth - bitmap.Width) / 2;
using (var fromStream = Image.FromStream(bmStream, true))
{
g.DrawImage(fromStream, new Point(widthOffset, heightOffset));
heightOffset += bitmap.Height;
}
}
}
}
}
img.Save(outputStream, ImageFormat.Jpeg);
}
}
return outputStream;
}
}
This works fine most of the times, however occasionally I come across a particularly large PDF file (40+ A4 pages) and then the time comes to img.Save(outputStream, ImageFormat.Jpeg) I get A generic error occurred in GDI+.
Is there a limitation either on the Image class or the memory stream that is causing this behaviour, or is there another problem going on here?
And is there a way to solve this without separating the pages (as that is a constraint I'm working under at the moment)?

Related

PDFiumSharp PDF to Image size

I am using PDFiumSharp to generate JPGs from PDF file. Here is my code:
using (WebClient client = new WebClient())
{
byte[] pdfData = await client.DownloadDataTaskAsync(pdfUrl);
using (var doc = new PdfDocument(pdfData))
{
int i = 0;
foreach (var page in doc.Pages)
{
using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, true))
using (var stream = new MemoryStream())
{
page.Render(bitmap);
bitmap.Save(stream);
...
i++;
}
}
}
}
The codes work very well, images are generated accurately. However, each JPG is about 2mb. With multi-page PDF, the overall image size adds up quickly. Is there any way to reduce the JPG file size? I only need the JPG for preview purposes, not for printing. So lower resolution or quality is fine.
When you call bitmap.Save(...), the resulting byte[] that gets put into the MemoryStream stream represents a BMP. You should convert it into JPG yourself.
public static byte[] Render(PdfDocument pdfDocument, int pageNumber, (int width, int height) outputSize)
{
var page = pdfDocument.Pages[pageNumber];
using var thumb = new PDFiumBitmap((int)page.Width, (int)page.Height, false);
page.Render(thumb);
using MemoryStream memoryStreamBMP = new();
thumb.Save(memoryStreamBMP);
using Image imageBmp = Image.FromStream(memoryStreamBMP);
using MemoryStream memoryStreamJPG = new();
imageBmp.Save(memoryStreamJPG, ImageFormat.Jpeg);
return memoryStreamJPG.ToArray();
}

Decoding Z64 (ZB64) string

I'm working on breaking down ZPL label definitions generated by NiceLabel label making software. For the most part I don't have to worry about decoding the Z64 because it is just encoded graphics and I don't need to change the underlying data.
However I have a line of text that is used as a graphic by the label for some reason probably due to Font's or something.
Anyways, the Z64 or ZB64 string is created by compressing the original data using LZ77 and encoding that as Base64 and then appending a CRC at the end.
TEST STRING FULL EXAMPLE:
:Z64:eJztkDFOxDAQRb81hRsULmBtruECyRwpZYpFGLmg5AhwFKMUuYal9CtL26QwHsbe3RMguv3lz9P85wD3/CWaiZ+56OjqWA44cwKIAyfeXXL1sQ7YWqd54czltTge+VOdOQsXFp8TrLUw9KEW3+6pLU4Zk3mC0ataonSEzU8JMywGCiFcue+c8YLGvYcLF5a+68WFhbvtRs5jdmVkWolj96vgXe/it7eucT+0+gxV5N5RrdTveQpevhnxO+BEfRe0xIzc/EbUzkn3lhLSIH6DdFeu+c39Hb7c7vksfrJryB8vu6A4cxE/NjpK1/6LkJZ3+nL1gaLt3D33/Ed+AehfkrY=:6C38
TEST STRING TARGET EXAMPLE:
eJztkDFOxDAQRb81hRsULmBtruECyRwpZYpFGLmg5AhwFKMUuYal9CtL26QwHsbe3RMguv3lz9P85wD3/CWaiZ+56OjqWA44cwKIAyfeXXL1sQ7YWqd54czltTge+VOdOQsXFp8TrLUw9KEW3+6pLU4Zk3mC0ataonSEzU8JMywGCiFcue+c8YLGvYcLF5a+68WFhbvtRs5jdmVkWolj96vgXe/it7eucT+0+gxV5N5RrdTveQpevhnxO+BEfRe0xIzc/EbUzkn3lhLSIH6DdFeu+c39Hb7c7vksfrJryB8vu6A4cxE/NjpK1/6LkJZ3+nL1gaLt3D33/Ed+AehfkrY=
My Code to Decode / Decompress:
static string DecompressZb64(string compressedString)
{
var b64 = SmartWarehouse.Shared.Utils.Parser.ConvertFromBase64(compressedString);
var encoding = new ASCIIEncoding();
var inBytes = Encoding.ASCII.GetBytes(b64);
var outBytes = new byte[inBytes.Length];
try
{
using (var memoryStream = new MemoryStream())
using (var decompressionStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
{
decompressionStream.Read(outBytes, 0, inBytes.Length);
}
return encoding.GetString(outBytes);
}
catch (Exception e)
{
// TODO: DOcument exception
Console.WriteLine(e.Message);
}
return string.Empty;
}
Current Exception:
Block length does not match with its complement.
Stacktrace:
at System.IO.Compression.Inflater.DecodeUncompressedBlock(Boolean& end_of_block)
at System.IO.Compression.Inflater.Decode()
at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length)
at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count)
at SmartWarehouse.Tools.Program.DecompressZb64(String compressedString) in C:\Users\[user_dir]\Source\Repos\Handheld.[user].[fork]\SmartWarehouse.Tools\Program.cs:line 511
UPDATE:
I was looking into this and I found this SO post it is essentially the same issue. So i did some more research and i found this article on a blog from 2007. Which discusses a workaround by skipping the first 2 bytes in the input array due to those bytes not actually being included in the RFC specification.
CODE CHANGE:
static string DecompressZb64(string compressedString)
{
var b64 = Convert.FromBase64String(compressedString);
var encoding = new ASCIIEncoding();
var outBytes = new byte[b64.Length - 2];
try
{
using (var memoryStream = new MemoryStream(b64))
{
memoryStream.ReadByte();
memoryStream.ReadByte();
using (var decompressionStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
{
decompressionStream.Read(outBytes, 0, b64.Length - 2);
}
}
return encoding.GetString(outBytes);
}
catch (Exception e)
{
// TODO: DOcument exception
Console.WriteLine(e.Message);
}
return string.Empty;
}
This code change doesn't actually produce an exception anymore however it doesn't decompress properly and returns this result:
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
That data looks fine to me, I got this image out of it:
It's a monochrome bitmap, one bit per pixel.
Your code didn't read until the end of the stream, there are actually 1280 bytes of image data, which can then be decoded to the image above.
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace ZB64Decode
{
public class Program
{
static void Main(string[] args)
{
//this is C#
var sb = new StringBuilder();
sb.AppendLine("^XA");
sb.AppendLine("^UT1,1");
sb.AppendLine("^Uh0,300,64,4000,2,0,F");
sb.AppendLine("^Uh1,300,64,4000,2,0,F");
sb.AppendLine("^UC1,0,1,0,0");
sb.AppendLine("^LH0,0");
sb.AppendLine("^LT0");
sb.AppendLine("^XZ");
sb.AppendLine("^XA");
sb.AppendLine("^PW3543");
sb.AppendLine("^LL512");
sb.AppendLine("^FO532,66^GFA,885,9272,76,:Z64:eJztWkFywyAMDMOBI0/gKTyNPC1P6RN6zKFTNxiCY0AgJE0nh+zJTc0GrdYES1wu/wWzHfiVIuLR2YZpB4FJpZH38UcouP6oRHddogqPEd/9f0WyLzyTGqpsVuLUj5tvoxv8tv3gqSa3WCSZxrjIoMgUzpAGoxnW2wZMdIFHG9tOEhQtekVyze7VSzYMQ/3DVIMTRt/sFtcUA0epZ2o28GCUfi3CCOjbDWHVhMaElRyOB1nss3+C7no7rAqf4DsTM+vr+A7VUYw4rTixeqAmqdUf2bKjESrz96LGwlQWNxRvPVHNw1N+2p9wJ30UWfkIfbKT5YQY1X/5gxVinMqtXCui53vjWVmMCEcmHS/EGOT1ecnKYoQudlXra3ONIphZ3Oh14H7rCzrKdDxXrijYLV3w5Socmr50HcixCUhfNBeQvkxIQPoiVOA92AkqPdISacwsImnMSklyiVgiu8FyF6+EnUbEXjk8SS4Rq+YUviuXyCOUH6IP14er5npX37/rmiO5rkqu90K/Q5Jcux0Ud0OekCwvuZ+Q3OdI7r9EDJZJJPerkvtoiUQWY0m+d0i+Dwk4/5BJ8P2R++p+enk3XIf5I3uSdYDX93gKTnUEywvypLdmBVlVgwInk1U1iFUcqibCKVo1AjHqhc1Qanm1W2qsq5FouFZqS677diyw2r7LcL0Vnjax7rTgHsEQQC+CUsDvl+8vpMYCOGTQogEwaALBLZo+hk2gpRbYpM1llnLpx4V6fMsQ0TQM6KaAmQqisC1ijWiyahwZ7jZUVxfZIcb0m1F96x12dnzALRx9GHf8x6cFWmzwkQAPH2EA4AE26PMh0lmTagLkMye+HDG5P4+HUCbVsHGZIqqzMLPb/wCr2VTW:718B");
sb.AppendLine("^PQ1,0,1,Y^XZ");
var zpl = sb.ToString();
//extract the Z64-String
var z64Data = "";
var bytesPerRow = 0;
foreach (var item in zpl.Split('^'))
{
if (item.StartsWith("GFA"))
{
var sp= item.Split(':');
z64Data = item.Substring(sp[0].Length);
bytesPerRow = Convert.ToInt32(sp[0].Split(',')[3]);
break;
}
}
//convert String to Bitmap
Bitmap decodedBitmap = null;
if (z64Data.StartsWith(":Z64"))
{
var imageData = DecompressZb64(z64Data.Substring(5));
int width = bytesPerRow * 8;
int height = imageData.Length / bytesPerRow;
decodedBitmap = ArrayToBitmap(imageData, width, height, PixelFormat.Format1bppIndexed);
}
Debug.WriteLine(decodedBitmap.Width + ":" + decodedBitmap.Height);
}
public static Bitmap ArrayToBitmap(byte[] bytes, int width, int height, PixelFormat pixelFormat)
{
var image = new Bitmap(width, height, pixelFormat);
var imageData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadWrite, pixelFormat);
try
{
Marshal.Copy(bytes, 0, imageData.Scan0, bytes.Length);
}
finally
{
image.UnlockBits(imageData);
}
return image;
}
public static byte[] DecompressZb64(string compressedString)
{
var b64 = Convert.FromBase64String(compressedString.Split(':')[0]).Skip(2).ToArray();
return Decompress(b64);
}
public static byte[] Decompress(byte[] data)
{
byte[] decompressedArray = null;
try
{
using (MemoryStream decompressedStream = new MemoryStream())
{
using (MemoryStream compressStream = new MemoryStream(data))
{
using (DeflateStream deflateStream = new DeflateStream(compressStream, CompressionMode.Decompress))
{
deflateStream.CopyTo(decompressedStream);
}
}
decompressedArray = decompressedStream.ToArray();
}
}
catch (Exception ex)
{
// do something !
}
return decompressedArray;
}
}
}

About analysing the photo with tesseract

I wrote this code for analyzing the numbers included in picture. It does not give any error while starting but it can not read the numbers. When I start program, it shows an empty MessageBox.
I want to read pictures like this:
The code:
private string FotoAnaliz()
{
FileStream fs = new FileStream("D:\\program_goruntusu.jpg", FileMode.OpenOrCreate);
//string fotopath = #"D:\\program_goruntusu.jpg";
Bitmap images = new Bitmap(fs);
using (var engine = new TesseractEngine(#"./tessdata", "eng"))
{
engine.SetVariable("tessedit_char_whitelist", "0123456789");
// have to load Pix via a bitmap since Pix doesn't support loading a stream.
using (var image = new Bitmap(images))
{
using (var pix = PixConverter.ToPix(image))
{
using (var page = engine.Process(pix))
{
sayı = page.GetText();
MessageBox.Show(sayı);
fs.Close();
}
}
}
}
return sayı;
}
Try PSM 10: Treat the image as a single character.
https://github.com/tesseract-ocr/tesseract/blob/master/doc/tesseract.1.asc

Tesseract can not read single number

I am trying to scan the numbers on screenshot but tesseract can read 2 number digit (ex.30,21,19) but it can not ready single digit (ex. 2,6,9) how can i fix that? I tried some solutions but i can not fix this problem.
private string FotoAnaliz()
{
FileStream fs = new FileStream("D:\\program_goruntusuasıl.png", FileMode.OpenOrCreate);
//string fotopath = #"D:\\program_goruntusu.jpg";
Bitmap images = new Bitmap(fs);
using (var engine = new TesseractEngine(#"./tessdata", "eng"))
{
engine.SetVariable("tessedit_char_whitelist", "0123456789");
// have to load Pix via a bitmap since Pix doesn't support loading a stream.
using (var image = new Bitmap(images))
{
using (var pix = PixConverter.ToPix(image))
{
using (var page = engine.Process(pix))
{
sayı = page.GetText();
MessageBox.Show(sayı);
fs.Close();
}
}
}
}
return sayı;
}

Getting bigger size image while scaling C#

I am trying to scale the original image to 50% and 25% and try to download the scaled image in MVC. I am using the below code which was taken from Google search.
public byte[] ScaleImageByPercent(byte[] imageBuffer, int Percent)
{
using (Stream imageStream = new MemoryStream(imageBuffer))
{
using (Image scaleImage = Image.FromStream(imageStream))
{
float scalePercent = ((float)Percent / 100);
int originalWidth = scaleImage.Width;
int originalHeight = scaleImage.Height;
int originalXPoint = 0;
int originalYPoint = 0;
int scaleXPoint = 0;
int scaleYPoint = 0;
int scaleWidth = (int)(originalWidth * scalePercent);
int scaleHeight = (int)(originalHeight * scalePercent);
using (Bitmap scaleBitmapImage = new Bitmap(scaleWidth, scaleHeight, PixelFormat.Format24bppRgb))
{
scaleBitmapImage.SetResolution(scaleImage.HorizontalResolution, scaleImage.VerticalResolution);
Graphics graphicImage = Graphics.FromImage(scaleBitmapImage);
graphicImage.CompositingMode = CompositingMode.SourceCopy;
graphicImage.InterpolationMode = InterpolationMode.NearestNeighbor;
graphicImage.DrawImage(scaleImage,
new Rectangle(scaleXPoint, scaleYPoint, scaleWidth, scaleHeight),
new Rectangle(originalXPoint, originalYPoint, originalWidth, originalHeight),
GraphicsUnit.Pixel);
graphicImage.Dispose();
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(scaleBitmapImage, typeof(byte[]));
}
}
}
}
When i use 3.4MB image its returning 4.7MB in 50% and even worst in 100% its returning 18 MB.
EDIT:
After getting the byte array i am downloading the image using below code. After downloading while i check the file size in disk its showing bigger size.
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(new MemoryStream(scaledBytes));
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
Am i doing the scaling correctly?. Which one i need to change get the lower size image while scaling using above functionality.
Your code works, I believe it's just a matter of image compression, basically you are pushing your byte array to your output stream as is, while you should save it as a jpeg. In my example I use a FileStream for simplicity, in your case you should use your output stream.
Give this a try (just drop any Jpg file on the compiled executable):
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string filePath = System.IO.Path.GetFullPath(args[0]);
byte[] originalImage = System.IO.File.ReadAllBytes(filePath);
byte[] resizedImage = ScaleImageByPercent(originalImage, 50);
using (Stream imageStream = new MemoryStream(resizedImage))
{
using (Image scaleImage = Image.FromStream(imageStream))
{
string outputPath = System.IO.Path.GetDirectoryName(filePath);
outputPath = System.IO.Path.Combine(outputPath, $"{System.IO.Path.GetFileNameWithoutExtension(filePath)}_resized.jpg");
using (FileStream outputFile = System.IO.File.Open(outputPath, FileMode.Create, FileAccess.Write))
{
scaleImage.Save(outputFile, ImageFormat.Jpeg);
}
}
}
}
public static byte[] ScaleImageByPercent(byte[] imageBuffer, int Percent)
{
using (Stream imageStream = new MemoryStream(imageBuffer))
{
using (Image scaleImage = Image.FromStream(imageStream))
{
float scalePercent = ((float)Percent / 100);
int originalWidth = scaleImage.Width;
int originalHeight = scaleImage.Height;
int originalXPoint = 0;
int originalYPoint = 0;
int scaleXPoint = 0;
int scaleYPoint = 0;
int scaleWidth = (int)(originalWidth * scalePercent);
int scaleHeight = (int)(originalHeight * scalePercent);
using (Bitmap scaleBitmapImage = new Bitmap(scaleWidth, scaleHeight, PixelFormat.Format24bppRgb))
{
scaleBitmapImage.SetResolution(scaleImage.HorizontalResolution, scaleImage.VerticalResolution);
Graphics graphicImage = Graphics.FromImage(scaleBitmapImage);
graphicImage.CompositingMode = CompositingMode.SourceCopy;
graphicImage.InterpolationMode = InterpolationMode.NearestNeighbor;
graphicImage.DrawImage(scaleImage,
new Rectangle(scaleXPoint, scaleYPoint, scaleWidth, scaleHeight),
new Rectangle(originalXPoint, originalYPoint, originalWidth, originalHeight),
GraphicsUnit.Pixel);
graphicImage.Dispose();
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(scaleBitmapImage, typeof(byte[]));
}
}
}
}
}
}
Here it is the result:
EDIT:
Ok for the webapi interface try doing like this:
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
using (Stream imageStream = new MemoryStream(resizedImage))
{
using (Image scaleImage = Image.FromStream(imageStream))
{
using (MemoryStream ms = new MemoryStream())
{
scaleImage.Save(ms, ImageFormat.Jpeg);
result.Content = new StreamContent(ms);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
}
}
}
return result;
I think that we can reduce size of image by change PixelFormat type.
You can refer to this Reducing Bitmap bit-size in C#

Categories