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.
Related
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.
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");
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?
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.
I have array of image bytes and I would like to set resolution. Original image can be JPEG, PNG, BMP. Output - PNG. I am using ImageMagic to convert image and do some manipulations.
using (var image = this.Convert(originalImage, height, width))
using (var stream = new MemoryStream())
{
image.Quality = 90;
image.Write(stream, MagickFormat.Png);
return stream.GetBuffer();
}
I tryed to modify image.GetExifProfile, but has no success (at least for PNG images).
I can't use any comandline tool (like ImageMagic or ExifTool) here.
There are 3 exiff tags I need to modify
XResolution
YResolution
ResolutionUnit
I can successfully achieve this with bitmap, but it also resource overhead (need to create MemoryStream ...).
I have found some Pdf specification, but it will consume time to make it all work.
Does any can point me to right direction?
Thanks.