how to convert Bitmap Image to jpeg2000 - c#

There are images which are in Bitmap format i need to convert it to jpeg2000 form. can you please tel me steps included in this.how can images can be converted from bmp to jpeg2000. how can i do this thank you in advance

You could use Magick.NET (https://github.com/dlemstra/Magick.NET).
using (MagickImage image = new MagickImage("input.bmp"))
{
image.Write("output.jp2");
}

You can use Jpeg2000.Net library. Disclaimer: I am working on this library, the library is commercial.
Here are basic samples for encoding of BMP image to JPEG 2000:
a. Lossless encoding
J2kImageData imageData = J2kImageData.FromImage("input.bmp");
imageData.Encode("output-lossless.j2k");
b. Encoding with compression
J2kImageData imageData = J2kImageData.FromImage("input.bmp");
var options30x = new J2kEncodingOptions
{
Codec = J2kCodec.J2k,
QualityMode = J2kQualityMode.CompressionRatio,
QualityValues = new float[] { 30 }
};
imageData.Encode(#"output-30x.j2k", options30x);

Not sure how exactly you would like to do it, however, you may want to look into ImageMagick features. http://www.imagemagick.org/script/jp2.php

Use FileStream .
byte[] raw = File.ReadAllBytes("pic.bmp");
using(Image img = Image.FromStream(new MemoryStream(raw)))
{
img.Save("pic.jp2", ImageFormat.Jpeg);
}

Related

How to convert a base64 source to image source in xamarin

I want to convert a svg base64 string to image and need to assign that to image source in Xamarin How to implement the same . Please help on this.
Var dImage="data:image/avg+XML;base64,PD94b.....=="
My base64 string is in the above format.I did the normal base64 to image convertion but it didn't worked.
Can you please try it with plain data (without mime-type,...):
=> Convert.FromBase64String("PD94b.....==");
var dImage = "data:image/avg+XML;base64,PD94b.....==";
var base64 = dImage.Split(',')[1];
byte[] bytes = Convert.FromBase64String(base64);
Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
}

Convert any image format to JPG

I have a web app that runs on Azure. The user can upload an image and I save that image for them. Now when I receive the image, I have it as byte[]. I would like to save it as JPG to save space. The source image could be anything such as PNG, BMP or JPG.
Is it possible to do so? This needs to run on Azure and I am using WebApps/MVC5/C#.
Thanks for any help.
Get the memorystream and then use System.Drawing
var stream = new MemoryStream(byteArray)
Image img = new Bitmap(stream);
img.Save(#"c:\s\pic.png", System.Drawing.Imaging.ImageFormat.Png);
The last line there where you save the file, you can select the format.
So the answers by #mjwills and Cody was correct. I still thought to put the two methods I exactly needed:
public static Image BitmapToBytes(byte[] image, ImageFormat pFormat)
{
var imageObject = new Bitmap(new MemoryStream(image));
var stream = new MemoryStream();
imageObject.Save(stream, pFormat);
return new Bitmap(stream);
}
Also:
public static byte[] ImgToByteArray(Image img)
{
using (MemoryStream mStream = new MemoryStream())
{
img.Save(mStream, img.RawFormat);
return mStream.ToArray();
}
}
Cheers everyone.

C# .svg file to System.Drawing.Image

I need to convert the selected .svg file to System.Drawing.Image object, so I can resize it and save it as .png. Can anyone help me with this?
Here is what I have so far:
Svg.SvgDocument svgDocument = SVGParser.GetSvgDocument(mPath);
image = svgDocument.Draw();
But it gives me out of memory error.
You can use the SVG Rendering Engine Lib:
Install-Package Svg
It's quite easy to draw images using it:
var svgDoc = SvgDocument.Open(imagePath);
using(var Image = new Bitmap(svgDoc.Draw()))
{
Image.Save(context.Response.OutputStream, ImageFormat.Png);
context.Response.ContentType = "image/png";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
}
In this example i'm using a handler to display the image on the browser but you can easily save it on some folder just by changing the first parameter of the Save method.
The resource Miljan Vulovic used is svg (https://archive.codeplex.com/?p=svg).
Link is only valid until July 2021, it might be available on GitHub by then, but I'm not sure.
Anyways his solution works for me.
So,
SVGParser.MaximumSize = new System.Drawing.Size(4000, 4000);
svgDocument = SVGParser.GetSvgDocument(mPath);
var bitmap = svgDocument.Draw();
image = bitmap;

C# iTextSharp Extracted CMYK images returns in RGB Format

I am using iTextsharp to extracted images from epaper PDF files, the images in the PDF files are in CMYK format, but the extracted images are in RGB. Please advice on this. Thanks in advance
int xrefIdx = ((PRIndirectReference)obj).Number;
PdfObject pdfObj = doc.GetPdfObject(xrefIdx);
PdfStream str = (PdfStream)pdfObj;
byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)str);
using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
{
var rawImage = System.Drawing.Image.FromStream(memStream);
rawImage.Save(#"e:\extractedimages.jpeg", ImageFormat.Jpeg);
}
Unfortunately, .NET isn't really up to the job for what you need to do as it really only works in RGB. Please see this answer to another question (https://stackoverflow.com/a/1773496/7122) which has more details.

Silverlight 4: How can I convert bmp byte array to png byte array?

I have a wcf service which returns a bmp in byte[]. However Silverlight's Image control doesnt support displaying bmp's so i need to convert the bmp byte[] to png or jpg byte[]. Is there a library out there which does this conversion? Or any other way of displaying the bmp byte[] on the silverlight client?
Thanks!
Update1
In order to achieve the conversion I would have done something like this in .NET
private byte[] ConvertBmpToJpeg(byte[] bmp)
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bmp)))
{
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
return ms.ToArray();
}
}
Since System.Drawing is not available in Silverlight, how do I achieve what the code does above in Silverlight?
Answer
using the library mentioned by dj kraze below-
ExtendedImage img = new ExtendedImage();
var bd = new BmpDecoder();
var je = new JpegEncoder();
bd.Decode(img, new MemoryStream(bitmapBytes));
MemoryStream ms = new MemoryStream();
je.Encode(img, ms);
BitmapImage bi = new BitmapImage();
bi.SetSource(new MemoryStream(ms.ToArray()));
display_ScreenShot.Source = bi;
Here is an even easier way of doing it..
This site may help out a lot
Image Converting

Categories