I want to fetch an image from database. For that I have created a byte array for an image, which is passed by a string, and now I want to convert that string into image format. I am assigning that image to a Jlabel field. The code is as follows:
try {
Blob image_vis = rs1.getBlob(10);
InputStream x=image_vis.getBinaryStream();
OutputStream out=new FileOutputStream(string_op);
byte[] bytes = string_op.getBytes();
String s=new String(bytes);
System.out.println(+s); //prints bytes for the string
ImageIcon icon_cap = new ImageIcon(string_op);
image_cap.setIcon(icon_cap); //prints nothing to Jlabel
//image_cap.setText(s); //prints a path of a image
}
Convert an ArrayBuffer or typed array to a Blob var array = new Uint8Array([0x04, 0x06, 0x07, 0x08]); var blob = new Blob([array]); PDF - Download JavaScript for free
In MySQL blob has a method: getBytes () This method Retrieves all or part of the BLOB value that this Blob object represents, as an array of bytes. Blob myblob= my_result_set.getBlob ("Database_Field"); int myblobLength = (int) myblob.length (); byte [] myblobAsBytes = myblob.getBytes (1, myblobLength); blob.free ();
If it is an image then you can use Image to convert the byte array to the displayable image, for Winforms. If you just have the byte array then put it into a MemoryStream and then use FromStream to read it. If it is on disk already then use FromFile instead.
This method Retrieves all or part of the BLOB value that this Blob object represents, as an array of bytes. free () method frees the blob object and free up the resources that was hold by it. I want to give you an another easy example so that you can get a idea on how to implement it. Assume that you are using these:
Blob blob = new javax.sql.rowset.serial.SerialBlob(bytes);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With