My app retrieves file content from remote server. File can be image or text. When the file is image it returns some string like this:
????\bNExif\0\0MM\0*\0\0\0\b\0\a\0.... and so on. As I understand it's an image but in other format (binary?).
So how can I convert that string to an image and set it to control as source?
Thanks.
I think you can do something like this:
MemoryStream ms = new MemoryStream(bytes);
BitmapImage bi = new BitmapImage();
bi.SetSource(ms);
Then if you have an Image element in XAML, say XamlImage:
XamlImage.Source = bi;
Related
I have a database program that stores images to the SQL DB and reads them back to be displayed in a WPF application. if i use Jpeg images it works fine, but if i use PNG images, which i wanted to use to try and keep the transparency ( which disappears anyway when stored ) most of the images come back corrupt.
this is the image that has been selected
i then save it to the db, and add the image the listview
then if i close the application and reload it, it pulls the image back from the db, you can see that it is corrupt in the listview
and then when i select it, the image control also shows the corrupted image
i am storing the image in code using a BitmapImage object, and use this to set the image.source, and also convert this to a byte[] for storing into the image field in the database.
i convert the bitmapimage to a Byte[] with the following line
command.Parameters.AddWithValue("#Image", ImageToByteArray(productImage.ProductImage));
and these are the functions to convert to and from a bitmapimage
private static BitmapImage BuildImage(byte[] image)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
MemoryStream mem = new MemoryStream(image);
bitmap.StreamSource = mem;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
//bitmap.Freeze();
return bitmap;
}
private static byte[] ImageToByteArray(BitmapImage image)
{
byte[] data;
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(image));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
data = ms.ToArray();
}
return data;
}
works fine with Jpegs, but then i have the white background to the images.
any help would be much appreciated.
"JpegBitmapEncoder" is a large clue. Surely you'd want "PNGBitmapEncoder"...?
As personal preference I would change your SQL data column to varbinary(MAX) and use stream and BinaryReader objects to upload the file.
However I think your issue is you are not using PngBitmapEncoder for the PNG you are using the JpegBitmapEncoder irrespective of filetype.
Hope this helps.
I am new to Windows 8 app development. In my app I need to display a GridView with images and title. The image URL and the title I get from the server as a XML data. The images are downloaded from the given URL and stored in a local directory. Now, when an image is downloaded I want to notify the GridView and update the particular image view with the downloaded image. I store the title and the local image URI in an ObservableCollection. The data source of the GridView is bound to this ObservableCollection, so once the XML data is downloaded I am able to update the title through the ObservableCollection. But i don't know how to update the images once they are downloaded.
Assuming that your image is saved in the local data folder (ApplicationData.Current.LocalFolder) - you can create a new BitmapImage this way:
var imagePathInLocalDataFolder = ?
var imageUri = new Uri("ms-appdata:///local/" + imagePathInLocalDataFolder, UriKind.Absolute);
var bitmapImage = new BitmapImage(new Uri(imageUri));
You can then assign the bitmapImage variable value to a property that you bind to an Image.Source - and you should see your image.
I am trying to resize a .tif image and then display it on the browser by converting it to a base64 string. Since ImageIo doesn't support TIF images by default, i have added imageio_alpha-1.1.jar(got it here - http://www.findjar.com/jar/geoserver/jai/jars/jai_imageio-1.1-alpha.jar.html). Now ImageIO is able to register the plugin, which i checked by doing this
String[] writerNames = ImageIO.getWriterFormatNames();
writerNames has TIF in it, this means ImageIO has registered the plugin.
I am resizing the image like this
Map resizeImage(BufferedImage imageData, int width, int height, String imageFormat){
BufferedImage thumbnail = Scalr.resize(imageData, Scalr.Method.SPEED, Scalr.Mode.FIT_EXACT ,
width, height, Scalr.OP_ANTIALIAS);
String[] writerNames = ImageIO.getWriterFormatNames();
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ImageIO.write(thumbnail, imageFormat, baos)
baos.flush()
byte[] imageBytes = baos.toByteArray()
baos.close()
return [imageBytes:imageBytes, imageFormat:imageFormat]
}
String encodeImageToBase64(byte[] imageData){
return Base64.encodeBase64String(imageData)
}
BufferedImage getBufferedImage(byte[] imageData){
ByteArrayInputStream bais = new ByteArrayInputStream(imageData)
BufferedImage bImageFromConvert = ImageIO.read(bais)
bais.close()
return bImageFromConvert
}
String resizeToDimensions(byte[] imageData, String imageFormat, int width, int height){
def bimg = getBufferedImage(imageData)
Map resizedImageData = resizeImage(bimg, width, height, imageFormat)
return encodeImageToBase64(resizedImageData.imageBytes)
}
now i am displaying the image like this
< img src = "data:image/tif;base64,TU0AKgAAAAgADAEAAAMAAA...." /> with this i get failed to load url message(on hovering)
as far as i know the base64 string usually starts with /9j/(may be i am wrong). when i am appending /9j/. I get an error - "image corrupt or truncated". I am not able to figure out the problem here, please help.
At first glance your use of the Data URI format looks correct -- try and narrow down exactly where the failure is.
I would recommend:
In your method where you return the string to the front end, I would recommend printing the entire thing out to the console to get the raw data in Data URI format.
Take the Data URI string, create a sample HTML file with the hard-coded value in it, try and load it... does the image display? If so, great, then your problem is with how you are streaming that back to the front end or how you are trying to load it. (Likely a JavaScript/DOM issue)
If that doesn't work, try and chop the Base64 section out of the example and save it into an example TXT file. In your Java code, load it, decode it and try and create an image out of it and write it back out to a TIFF -- if that didn't work, then there is something wrong with your Base64 handling and the encoding is invalid most likely.
Getting that far should answer most of the questions.
Actually now that I think about it, try and use ImageIO to read the image into a BufferedImage, then process it with imgscalr, then immediately call ImageIO.write and try and write it out to a new TIF someplace else and make sure the ImageIO decoding/encoding process is working correctly.
Hope that helps!
I have saved an image in database using following code.
ofd.ShowDialog()
vrPicHolder = IO.File.ReadAllBytes(ofd.FileName)
Dim drPic As DataRow
drPic = DsPic.tblPicTest.NewRow
drPic.Item("Picture") = vrPicHolder
DsPic.tblPicTest.Rows.Add(drPic)
taPic.Update(DsPic.tblPicTest)
Now I want to display this image in a picture box. I tried
PictureBox1.Image = Image.FromFile(vrPicHolder)
But it says can not convert Byte() to string. Please advise how to load this picture.
Thanks
Furqan
The method you're calling expects a string that's the filename of an image. You need to pass it the filename of an image that is a BMP, GIF, JPEG, PNG or TIFF format.
Image.FromFile Method (String)
What you should be doing is this:
Dim pictureBytes as New MemoryStream(vrPicHolder)
PicutureBox1.Image = Image.FromStream(pictureBytes)
Image.FromStream Method (Stream)
This is because Image.FromFile Method expects FilePath as String. Its like importing a file from some given path.See Image.FromFile Method (String)
Image.FromStream Method (Stream) is the remedy for this.
One more thing ,you should not set the complete Image to the database . Instead save the image into some physical path and refer this path from the database.
Check this out:
http://windevblog.blogspot.com/2008/08/convert-image-to-byte-array-and-vice.html
I have a requirement to be be able to embed scanned tiff images into some SSRS reports.
When I design a report in VS2005 and add an image control the tiff image displays perfectly however when I build it. I get the warning :
Warning 2 [rsInvalidMIMEType] The value of the MIMEType property for the image ‘image1’ is “image/tiff”, which is not a valid MIMEType. c:\SSRSStuff\TestReport.rdl 0 0
and instead of an image I get the little red x.
Has anybody overcome this issue?
Assuming you're delivering the image file via IIS, use an ASP.NET page to change image formats and mime type to something that you can use.
Response.ContentType = "image/png";
Response.Clear();
using (Bitmap bmp = new Bitmap(tifFilepath))
bmp.Save(Response.OutputStream, ImageFormat.Png);
Response.End();
I have been goggling fora solution on how to display a TIFF image in a SSRS report but I couldn't find any and since SSRS doesn's support TIFF, I thought converting the TIFF to one of the suppported format will do the trick. And it did. I don't know if there are similar implementation like this out there, but I am just posting so others could benefit as well.
Note this only applies if you have a TIFF image saved on database.
Public Shared Function ToImage(ByVal imageBytes As Byte()) As Byte()
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(imageBytes)
Dim os As System.IO.MemoryStream = New System.IO.MemoryStream()
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
img.Save(os, System.Drawing.Imaging.ImageFormat.Jpeg)
Return os.ToArray()
End Function
Here’s how you can use the code:
1. In the Report Properties, Select Refereneces, click add and browse System.Drawing, Version=2.0.0.0
2. Select the Code Property, Copy paste the function above
3. Click Ok
4. Drop an Image control from the toolbox
4.1. Right-Click the image and select Image Properties
4.2. Set the Image Source to Database
4.3. In the Use this field, Click expression and paste the code below
=Code.ToImage(Fields!FormImage.Value)
4.4. Set the appropriate Mime to Jpeg
Regards,
Fulbert
Thanks Peter your code didn't compile but the idea was sound.
Here is my attempt that works for me.
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "image/jpeg";
Response.Clear();
Bitmap bmp = new Bitmap(tifFileLocation);
bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
Response.End();
}