I am trying to convert a varbinary to an image in my silverlight project.
First I get the Binary from my database in my service.
[OperationContract]
public byte[] getAfbeelding(int id)
{
var query = (from p in dc.Afbeeldings
where p.id == id
select p.source).Single();
byte[] source = query.ToArray();
Then I try to convert the varbinary to an image, using code found on StackOverflow:
public static string convertToImage(byte[] source)
{
MemoryStream ms = new MemoryStream(source);
Image img = Image.FromStream(ms);
return img.Source.ToString();
}
But as it turns out, the silverlight Image does not have a .FromStream, I tried all the examples found in this thread but NONE of them work in silverlight.
'System.Windows.Controls.Image' does not contain a definition for 'FromStream'
So yeah, I'm kinda lost and am not sure what to do.
Any ideas on how to do this in silverlight?
You're almost right. The following code should be all you need:
var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(imageData));
newImage.Source = bitmapImage;
where imageData is of type byte[] and newImage is the image you want to update.
you sould have a look at WriteableBitmap.
there is a pretty nice set of extensions freely available on codepelex or Nuget
Related
So I am trying to convert an array of bytes to an image but I get this error:
System.ArgumentException: 'Parameter is not valid.' The byte[] comes from a database in SQL server where the data type is "image".
I have already tried every solution that I have found offered in other questions in StackOverflow and they have not worked for me; Here is my code:
//biblioteca.herramientas is where I make the connection with the server
DataSet dataset_Image;
dataset_Image = Biblioteca.Herramientas(string.Format("SELECT * FROM Image WHERE id_Image = " + 1));
array = (byte[])dataset_Image.Tables[0].Rows[0]["image"];
public byte[] TheImage
{
set
{
theImage = ImageConversor.ByteArrayToImage(value);
PictureBox2.Image = theImage;
}
}
This is where I have the error:
public class ImageConversor
{
public static Image ByteArrayToImage(byte[] byteArrayIn)
{ //HERE THE ERROR APPEARS
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
}
The following picture is the SQL table that I am accessing to:
And that is all, if you need more info I will share it, thank you for your time, hope you have a good day. (:
EDIT:
The code works fine if I add the picture directly from SQLS, changing the column "image"(where the picture is stored) datatype from "image" to "varbinary(MAX)" and then using the following code to insert the image:
insert into Image select * from openrowset (bulk N'picture directory', single_blob) as image
The problem is that I can not use that to add the picture, but I dont know, maybe it helps to solve this problem :/
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;
i have images on a webserver and I convert they in Base64 to receive it on a .Net application. I use this code:
$imagedata = file_get_contents($local_uri);
$d = base64_encode($imagedata);
It works perfekt.
Now i had the idea to resize the images to save time for transmission.
So I resize the images with a simple PHP-Framework from http://deruwe.de/vorschaubilder-einfach-mit-php-realisieren-teil-2.html
On behind it use ImageJPEG to "return" or create the edited image.
On an other post here i read that if you want to use ImageJPEG and translate it on Base64 you have to use this code:
ob_start();
$thumbnail->output(false, false);
$p = ob_get_contents();
ob_end_clean();
$d = base64_encode($p);
The "new" Base64 code seems legit, because if I try to illustrate it with http://www.freeformatter.com/base64-encoder.html, I got a legit image.
BUT due an unknown reason my .Net application will throw an System.NotSupportedException (No imaging component suitable to complete this operation was found.) and don't like the "new" one.
This is my .Net function to convert Base64 String in BitmapImage:
public static BitmapImage getImage(String s)
{
byte[] binaryData = Convert.FromBase64String(s);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = new MemoryStream(binaryData);
bi.EndInit();
return bi;
}
Any idea whats gone wrong or how to fix it?
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);
}
Im loading an image from a SQL CE db and then trying to load that into a PictureBox.
I am saving the image like this:
if (ofd.ShowDialog() == DialogResult.OK)
{
picArtwork.ImageLocation = ofd.FileName;
using (System.IO.FileStream fs = new System.IO.FileStream(ofd.FileName, System.IO.FileMode.Open))
{
byte[] imageAsBytes = new byte[fs.Length];
fs.Read(imageAsBytes, 0, imageAsBytes.Length);
thisItem.Artwork = imageAsBytes;
fs.Close();
}
}
and then saving to the Db using LINQ To SQL.
I load the image back like so:
using (FileStream fs = new FileStream(#"C:\Temp\img.jpg", FileMode.CreateNew ,FileAccess.Write ))
{
byte[] img = (byte[])encoding.GetBytes(ThisFilm.Artwork.ToString());
fs.Write(img, 0, img.Length);
}
but am getting an OutOfMemoryException. I have read that this is a slight red herring and that there is probably something wrong with the filetype, but i cant figure what.
Any ideas?
Thanks
picArtwork.Image = System.Drawing.Bitmap.FromFile(#"C:\Temp\img.jpg");
Based on the code you provided it seems like you are treating the image as a string, it might be that data is being lost with the conversion from byte[] to string and string to byte[].
I am not familiar with SQL CE, but if you can you should consider treating the data as a byte[] and not encoding to and from string.
I base my assumption in this line of code
byte[] img = (byte[])encoding.GetBytes(ThisFilm.Artwork.ToString());
The GDI has the bad behavior of throwing an OOM exception whenever it is not capable of understanding a certain image format. Use Irfanview to see what kind of image that really is, it is likely GDI can't handle it.
My advice would be to not use a FileStream to load images unless you need access to the raw bytes. Instead, use the static methods provided in Bitmap or Image objects:
Image img = Image.FromFile(#"c:\temp\img.jpg")
Bitmap bmp = Bitmap.FromFile(#"c:\temp\img.jpg")
To save the file use .Save method of the Image or Bitmap object:
img.Save(#"c:\temp\img.jpg")
bmp.Save(#"c:\temp\img.jpg")
Of course we don't know what type ArtWork is. Would you care to share that information with us?