using DataURL in VB.NET - vb.net

happy new year!
I just want to generate DataURL type data in VB.Net and also want to load string type DataURL to an image.
In other words, I want to convert an image in PictureBox(or Image) into string type DataURL vice versa.
The purpose of this kind of doubtful procedure is to save string data and image data into one single file.
Thank you!

Something like this:
Use this function to get a Base64 string representation of the Image
Public Function ToBase64String(ByVal aImage As Image) As String
Using stream = New System.IO.MemoryStream
aImage.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
Return Convert.ToBase64String(stream.ToArray)
End Using
End Function
Then use this to display it:
<img src="data:image/png;base64, Base64StringHere" />

Related

PNG file is corrupt when reading back from SQL

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.

Using richtextbox instead of File.WriteBytes

I am using this code now:
Dim data() As Byte = File.ReadAllBytes("D:\desktop\image.png")
File.WriteAllBytes("D:\desktop\file.txt", data)
Instead of File.WriteAllBytes (writing to a file) I want to write those bytes to a richtextbox, is this possible?
Thanks!
you can use like the following
Dim data() As Byte = File.ReadAllBytes("K:\sample.txt")
ritchText.Text = System.Text.Encoding.UTF8.GetString(data)
If you give the input as image file then you will get only Unicode characters. if you give textfile like the above then will give the exact content of the input file.

Binary to image in Windows Phone 7

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;

Loading byte data to a picture box

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

Displaying Tiff files in SSRS reports

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();
}