Convert bitmap to byte[] - silverlight

I am developing a LightSwitch application that generates barcodes (QR images) for tickets. I am calling an encode function that converts text to bitmap.
I just need to save this in an LightSwitch Image field.
I have this:
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
EditableImage image = qrCodeEncoder.Encode(data);
I want this:
ticket.QRImage = .....???
I am using this library for the QR
http://www.jeff.wilcox.name/2009/09/quick-read-silverlight-barcodes/
http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library

You can get the bytes by calling image.GetStream(), and then using one of the standard methods to get the bytes out of the stream (see How to convert an Stream into a byte[] in C#?)

Related

create a picture shape from an array

I am trying to find an example, or documentation, on creating a picture (floating shape object) inside the Excel sheet. The source is supposed to be a numeric bitmap data, stored in a VBA array acquired using external I/O libraries. Using Excel cells as an intermediary storage is possible, but not desired, since the RGB bitmap data is expected to be huge.
The task itself seems to be extremely simple in matlab-like environments, or python. But I just have no Idea how to make it in Excel and VBA without importing an independent image file from the file system.
In terms of storing the file, how huge is 'huge'? If you convert the image into Base64, it'll be a fairly trivial task to split it up amongst the cells and then reconstitute it when converting it into an image.
Alternatively, you can store the Base64 string in a standard module - I'm currently doing much the same thing, but my image only clocks in at 100kb (better to save it as a PNG rather than BMP).
In terms of converting the Base64 string to an image, the Windows Image Acquisition COM object will convert a byte array into a stdPicture image type (and further to my point above, it will also accept PNG files...]. The following function accepts a Base64 string, converts it into a byte array, and returns an stdPicture object:
Function Base64toStdPicture(ByVal Base64Code As String) As StdPicture
Dim ImgVector As Object
Dim Node As Object
Set ImgVector = CreateObject("WIA.Vector")
Set Node = CreateObject("Msxml2.DOMDocument.3.0").createElement("base64")
Node.DataType = "bin.base64"
Node.Text = Base64Code
ImgVector.BinaryData = Node.nodeTypedValue
Set Base64toStdPicture = ImgVector.BinaryData.Picture
Set Node = Nothing
Set ImgVector = Nothing
End Function
From that point, you can out it in an image control, or copy it to / from the clipboard, etc.

Convert Mat to byte[] - .net core

I'm trying to use EmgCV with .netCore and I want to post my image to an server.
How can I convert my Image (Mat or Image<Bgr, byte>) to byte[].
I cant convert it to Bitmap first, since im using .netcore and not framework. That's the way how I convert my byte[] to a Mat:
byte[] bytes = await response.Content.ReadAsByteArrayAsync();
Mat image2 = new Mat(4000, 6000, DepthType.Cv8U, 3);
CvInvoke.Imdecode(bytes, ImreadModes.AnyColor, image2);
Can someone help me to do it the other way round? I tried using this, but this doesn't work:
Image<Bgr, byte> afterImage = diff.ToImage<Bgr, byte>();
byte[] afterbytes = afterImage.Bytes;
Thanks in advance!
I think that the bytes you receive are not raw bitmap bytes. Instead you receive a jpeg or png encoded image. Thats why you have to use Imdecode.
EmguCV also offers method Imencode to encode images into various formats.
Example below encodes image2 Mat as jpeg bytes. Can also be png or something else when supported by EmguCV.
var buffer = new VectorOfByte();
CvInvoke.Imencode(".jpg", image2, buffer); //Must use .jpg not jpg
byte[] jpgBytes = buffer.ToArray();
Instead of Mat you can also pass Image<TColor,TDepth>
I found a solution by myself:
Image<Bgr, byte> afterImage = afterImageMat.ToImage<Bgr, byte>();
byte[] afterbytes = afterImage.ToJpegData();

How to convert a byte[] to a BufferedImage in Java?

I'm posting this thread because I have some difficulties to deal with pictures in Java. I would like to be able to convert a picture into a byte[] array, and then to be able to do the reverse operation, so I can change the RGB of each pixel, then make a new picture. I want to use this solution because setRGB() and getRGB() of BufferedImage may be too slow for huge pictures (correct me if I'm wrong).
I read some posts here to obtain a byte[] array (such as here) so that each pixel is represented by 3 or 4 cells of the array containing the red, the green and the blue values (with the additional alpha value, when there are 4 cells), which is quite useful and easy to use for me. Here's the code I use to obtain this array (stored in a PixelArray class I've created) :
public PixelArray(BufferedImage image)
{
width = image.getWidth();
height = image.getHeight();
DataBuffer toArray = image.getRaster().getDataBuffer();
array = ((DataBufferByte) toArray).getData();
hasAlphaChannel = image.getAlphaRaster() != null;
}
My big trouble is that I haven't found any efficient method to convert this byte[] array to a new image, if I wanted to transform the picture (for example, remove the blue/green values and only keeping the red one). I tried those solutions :
1) Making a DataBuffer object, then make a SampleModel, to finally create a WritableRaster and then BufferedImage (with additional ColorModel and Hashtable objects). It didn't work because I apparently don't have all the information I need (I have no idea what's the Hashtable for BufferedImage() constructor).
2) Using a ByteArrayInputStream. This didn't work because the byte[] array expected with ByteArrayInputStream has nothing to do with mine : it represents each byte of the file, and not each component of each pixel (with 3-4 bytes for each pixel)...
Could someone help me?
Try this:
private BufferedImage createImageFromBytes(byte[] imageData) {
ByteArrayInputStream bais = new ByteArrayInputStream(imageData);
try {
return ImageIO.read(bais);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
I have tried the approaches mentioned here but for some reason neither of them worked. Using ByteArrayInputStream and ImageIO.read(...) returns null, whereas byte[] array = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); returns a copy of the image data, not a direct reference to them (see also here).
However, the following worked for me. Let's suppose that the dimensions and the type of the image data are known. Let also byte[] srcbuf be the buffer of the data to be converted into BufferedImage. Then,
Create a blank image, for example
img=new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Convert the data array to Raster and use setData to fill the image, i.e.
img.setData(Raster.createRaster(img.getSampleModel(), new DataBufferByte(srcbuf, srcbuf.length), new Point() ) );
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
byte[] array = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(pixelArray, 0, array, 0, array.length);
This method does tend to get out of sync when you try to use the Graphics object of the resulting image. If you need to draw on top of your image, construct a second image (which can be persistant, i.e. not constructed every time but re-used) and drawImage the first one onto it.
Several people upvoted the comment that the accepted answer is wrong.
If the accepted answer isn't working, it may be because Image.IO doesn't have support for the type of image you're trying, for example tiff images.
To make it work, you need to add an extra jar to handle the image type.
You can add jai-imageio-core-1.3.1.jar to your classpath with:
<!-- https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core -->
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version>
</dependency>
To add support for:
wbmp
bmp
pcx
pnm
raw
tiff
gif (write)
You can check the list of supported formats with:
for(String format : ImageIO.getReaderFormatNames())
System.out.println(format);
Note that you only have to drop the jar (jai-imageio-core-1.3.1.jar for example) into your classpath to make it work.
Other projects that add additional support for image types include:
https://github.com/haraldk/TwelveMonkeys
https://github.com/geosolutions-it/imageio-ext
The approach by using ImageIO.read directly is not right in some cases. In my case, the raw byte[] doesn't contain any information about the width and height and format of the image. By only using ImageIO.read, It is impossible for the program to construct a valid image.
It is necessary to pass the basic information of the image to BufferedImage object:
BufferedImage outBufImg = new BufferedImage(width, height, bufferedImage.TYPE_3BYTE_BGR);
Then set the data for the BufferedImage object by using setRGB or setData. (When using setRGB, it seems we must convert byte[] to int[] first. As a result, it may cause performance issues if the source image data is big. Maybe setData is a better idea for big byte[] typed source data.)

Java - byte[] to FileItem

I'm using JasperReports to generate PDFs, and it gives me the PDF as a byte array, byte[].
I want to pass the raw bytes to another function that needs the file in terms of a FileItem object. In particular, the FileItem is from the Apache Commons library org.apache.commons.fileupload.FileItem.
// the function I want to pass it into
public DocumentDO toDocumentDO(FileItem fileItem);
Is there any way to do that or is it not possible (ie. the byte[] doesn't contain the metadata needed for it to be a FileItem like filename, mime type, etc)?
Your byte array is just what it is - bunch ow raw bytes, all meta data that you mentioned needs to be provided separately unless you read it into the file and then parse the file for the embedded meta information

WPF BitmapImage and TIFF with CMYK + Alpha

I am using this code snippet to load various image files:
BitmapImage bitmap = new BitmapImage ();
bitmap.BeginInit ();
bitmap.UriSource = new System.Uri (path);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit ();
This works fine for TIFF files stored as RGB, RGB+Alpha and CMYK. However, if I try to load a TIFF file using CMYK colors and an alpha channel, I get an exception (the file format is not recognized as being valid by the decoder).
I was previously using the FreeImage library and a thin C# wrapper on top of it. FreeImage 3.x has partial support for this kind of image format, i.e. I had to load the TIFF twice, once as CMYK without transparency and once as RGB+Alpha; this trick is needed since FreeImage only gives access to at most 4 simultaneous color channels.
I'd like to know if there is a supported way to load CMYK+Alpha bitmaps? Either directly in C# or by going through some interop code, but preferably without having to use a third-party DLL (other than the .NET 4 framework libraries).
An example of such a TIFF file can be found here.
EDIT : I can no longer reproduce the problem, the following code works just fine:
BitmapImage bitmap = new BitmapImage ();
bitmap.BeginInit ();
bitmap.UriSource = new System.Uri (path);
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.EndInit ();
byte[] pixels = new byte[bitmap.PixelHeight*bitmap.PixelWidth*5];
bitmap.CopyPixels (pixels, bitmap.PixelWidth * 5, 0);
But I am still stuck: how can I find out that the source image was encoded as CMYK plus Alpha channel? When looking at the Format property, I get only the information that the image has 40 bits per pixel. All the interesting stuff is stored in the following non-public properties:
bitmap.Format.FormatFlags == IsCMYK | NChannelAlpha;
bitmap.Format.HasAlpha == true;
Is there any official way of getting to them, without resorting to reflection?
I can only say this because I've had the issues with some files: it might be a better way to convert the tiff to png24 first and then load it up.
Even Photoshop puts up a warning if a user tries to save a CMYK Tiff file and ticks 'Transparency': "Many programs do not support transparency in TIFF. Save transparency information?"
So converting prior to opening might be the safe way to go.
Maybe http://msdn.microsoft.com/en-us/library/system.drawing.imageconverter.aspx would do it but I doubt it, you probably need some extra piping.
HTH.
It's only a guess, but GDI+ might be able to load such files.
System.Drawing.Image etc.
There is an interop class which can render GDI+ images in WPF.
I found another question on SO which linked to this library:
http://freeimage.sourceforge.net/
Good Tiff library for .NET
I hope this might help.

Resources