PDFsharp MigraDoc add runtime generated bitmap to table cell - c#

I wanted to use PDFsharp and MigraDoc to generate PDFs. As far as now it all works perfectly.
Now I have come up with an idea to create a bitmap during runtime and add it to one of my table cells.
I've read that it is possible to add bitmaps from resource so, without having them on the harddrive.
See: http://www.pdfsharp.net/wiki/MigraDoc_FilelessImages.ashx
This is my try to adapt it to my small project:
Code for creating the Bitmap:
Bitmap GreenDot = new Bitmap(32,32);
Graphics GreenDotGraphics = Graphics.FromImage(GreenDot);
GreenDotGraphics.FillEllipse(Brushes.Green,0,0,32,32);
//The next step will be converting the Bitmap to an byte[]
var byteGreenDot = ImageToByte(GreenDot);
//Now converting it to string as seen in the WikiPage
string stringGreenDot = Convert.ToBase64String(byteGreenDot);
string FinalGreenDot = "base64:"+ stringGreenDot;
//Now creating a table
.
.
.
cell = MyRow.Cell[1];
cell.AddImage(FinalGreenDot);
.
.
.
Code for converting bitmap to byte[]
public static byte[] ImageToByte(System.Drawing.Image img)
{
using(var ms = new MemoryStream())
{
img.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
return ms.ToArray();
}
}
As I run the code I'll get a warning saying "warning: Image 'base64:Qk02E[...]==' not found." (the base64 string was truncated for this post).
I guess that I don't convert it correctly to byte[].
Can someone get me on the right track?

Note that you are using a feature that was introduced with version 1.50.
When using version 1.32 or earlier, you get the warning you mention.
Your code works as expected when using version 1.50 beta 2 or later.

Related

How can I get image content as pixel and create image from it in ASP.NET Core 6 Web API?

I try to save my images on my server, but I can't let my server save file and virus because of that I want to get image content as pixels of rgb and after that I create image by myself.
I can't use bitmap (or other type in C# like bitmapImage, ... etc) and I don't know how I can do this with sixlabors.ImageSharp.
I have some code that I tried but I can't implement the exact logic that I want (code shown here):
[HttpPost("[action]")]
public async Task<IActionResult> Get([FromForm] ImageFormat file)
{
await using var memoryStream = new MemoryStream();
await file.File.CopyToAsync(memoryStream);
IImageFormat format;
using (var image = Image.Load(memoryStream.ToArray(), out format))
{
using (var output = new MemoryStream())
{
image.Save(output, format);
var responseType = format.Name.ToLower();
return File(output.ToArray(), "application/octet-stream", file.File.FileName);
}
}
return null;
}
Can anybody help me with this problem?
i don't see a reason to convert image into image: there are several format zip-algorythms etc.wich you have to support in that case. example jpg is not bitmap, there is convertion issue - quality of image becomes less each conversion time. Image itself is not executable - it can be used only as container for virus body, can't harm your OSystem itself, another executable part should works somewhere.
But even if you would like to store images on disk, in other format - you can convert image to base64 text (one line of code, like example) - it less harmful and well known way to work with any file type. you can zip image by cszip, you can change file name and extension to hide file type.
I don't see a reasson to convert one image to another for this scenario/task.

System.Drawing.Common.Bitmap cross platform alternatives

I am looking to convert PDF files into images. Docnet is able to convert the pdf into bytes[] and their samples show how to save this byte[] into an image file using Bitmap. Documentation
However, the solution won't work on linux machine since Bitmap requires few libraries pre-installed on the system.
I've tried ImageSharp to convert the byte[] using SixLabors.ImageSharp.Image.Load<Bgra32>(rawBytes), however, it throws Unhandled exception. SixLabors.ImageSharp.InvalidImageContentException: PNG Image does not contain a data chunk.
Does anyone knows any alternative to achieve this.
PS - I'm open to explore any other cross platform FREE supported alternatives to convert PDF files to images.
This works fine with ImageSharp assuming Docnet works then ImageSharp will work fine for you.
The trick is you want to be using the Image.LoadPixelData<Bgra32>(rawBytes, width, height); API not the Image.Load<Bgra32>(encodedBytes); one.
using Docnet.Core;
using Docnet.Core.Models;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using var docReader = DocLib.Instance.GetDocReader(
"wikipedia_0.pdf",
new PageDimensions(1080, 1920));
using var pageReader = docReader.GetPageReader(0);
var rawBytes = pageReader.GetImage();
var width = pageReader.GetPageWidth();
var height = pageReader.GetPageHeight();
// this is the important line, here you are taking a byte array that
// represents the pixels directly where as Image.Load<Bgra32>()
// is expected an encoded image in png, jpeg etc format
using var img = Image.LoadPixelData<Bgra32>(rawBytes, width, height);
// you are likely going to want this as well otherwise you might end up with transparent parts.
img.Mutate(x => x.BackgroundColor(Color.White));
img.Save("wikipedia_0.png");

How convert Image to png with imagemagik lib in C#?

I've write a aws Lambda to convert my images.
The process of the lambda function is to take input images of any format and in the case of non-png images, convert them to jpeg otherwise add some data and convert it to png by adding transparency. Finally save the generated images on s3.
I write a function in C# using a imagemagick library in .Net Core.
I've a problem with png conversion. The result image have a dashed border!
This is my method to PNG convert.
private MemoryStream ConvertToPNG(MemoryStream inputStream)
{
MemoryStream outputStream = new MemoryStream();
inputStream.Position = 0;
using (MagickImage image = new MagickImage())
{
image.Read(inputStream);
image.Quality = 90;
image.TransformColorSpace(ColorProfile.SRGB, ColorProfile.AdobeRGB1998);
image.Settings.Compression = CompressionMethod.NoCompression;
image.Format = MagickFormat.Png64;
image.Write(outputStream);
}
return outputStream;
}
I tried with another stack, using Pillow library in py and work, generated image is great. Unfortunately I am forced to use C # and .Net for obvious reasons.
Has anyone ever had this problem?
Thanks everyone for the contribution.

A generic error occurred in GDI+ when converting image to array to image

I have here existing code that I need to fix because for unknown reasons, our .dll files were probably replaced.
I am having problems on using the image for OCR (Python Tesseract). Below is the how the code works:
ImageConverter ic = new ImageConverter();
byte[] imgArray = (byte[])ic.ConvertTo(image, typeof(byte[]));
I am passing the image to make it into byte[] then pass it on the API. Then the API converts the array to image:
public Bitmap ConvertToImage(byte[] arr)
{
using (var ms = new MemoryStream(arr))
{
return new Bitmap(ms);
}
}
As an example, when I use Bitmap img2 = ConvertToImage(imgArray);, it gives me the error
I only get the GDI error when I try to use the image from the converted array. But when I use the straight image file (open file dialog) there seems to be no problem.
I can't really change the code, so can anyone suggest a solution? Or what's the problem when I used the array to image file?

Token not expected processing PDF using PDFsharp

I have two very similar pdf files. But if i try to process the first one it throws "Token '373071' was not expected" exception but for other one I can execute the code completely. Below is my code
class Program
{
static void Main(string[] args)
{
int bufferSize = 20480;
try
{
byte[] byteBuffer = new byte[bufferSize];
byteBuffer = File.ReadAllBytes(#"..\..\Fail.pdf");
MemoryStream coverSheetContent = new MemoryStream();
coverSheetContent.Write(byteBuffer, 0, byteBuffer.Length);
int t = PdfReader.TestPdfFile(coverSheetContent);
PdfReader.Open(coverSheetContent);
}
catch (Exception ex)
{
}
}
}
I've also added those PDF files. Well, those PDFs are row input for me I do not know where they got created or who does.
Fail.pdf
Success.pdf
There are very less information about PDFsharp please do help me to solve the problem.
The SAP tool that was used to create the PDF files adds many filling bytes after the "%%EOF" marker. PDFsharp up to version 1.32 expects the %%EOF marker within the trailing 130 bytes of the file.
You can modify the method ReadTrailer() in class Parser to search a larger area.
An implementation that searches the complete file can be found here:
http://forum.pdfsharp.net/viewtopic.php?p=583#p583
BTW: You can open the PDF like this:
var doc = PdfReader.Open(#"..\..\fail.pdf");
No need to allocate a buffer that will never be used, no stream needed.
Update: Since 2014 PDFsharp searches the complete PDF file if the "%%EOF" marker cannot be found near the end of the file. So if you are using PDFsharp 1.50 or newer it is no longer necessary to download and modify the code. Those who still use PDFsharp 1.32 or even older versions still have to modify the source.

Categories

Resources