how to display image if knows image data as binary? - c#

If I can retrieve image data from sql-server database, which stored as image type. How can I display the image in my web application using c#? Thanks in advance.

You have to create an http handler that returns the image
public void ProcessRequest(HttpContext context)
{
Byte[] yourImage = //get your image byte array
context.Response.BinaryWrite(yourImage);
context.Request.ContentType = "image/jpeg";
context.Response.AddHeader("Content-Type", "image/jpeg");
context.Response.AddHeader("Content-Length", (yourImage).LongLength.ToString());
con.Close();
context.Response.End();
context.Response.Close();
}
You can can do that by creating a GenericHandler file type from the visual studio and add the previous code in then you can call you can write the url of the generic handler as the image source

MemoryStream ms = new MemoryStream(byteArrayFromDB);
Image returnImage = Image.FromStream(ms);

You may want to look at this as well
byte[] imageBytes = (byte[]) imageReader.GetValue(0);
MemoryStream ms = new MemoryStream(imageBytes);
FileStream fs = File.OpenWrite(imagePath);
fs.Write(ms.GetBuffer(), 0, ms.Position());

Related

Convert any image format to JPG

I have a web app that runs on Azure. The user can upload an image and I save that image for them. Now when I receive the image, I have it as byte[]. I would like to save it as JPG to save space. The source image could be anything such as PNG, BMP or JPG.
Is it possible to do so? This needs to run on Azure and I am using WebApps/MVC5/C#.
Thanks for any help.
Get the memorystream and then use System.Drawing
var stream = new MemoryStream(byteArray)
Image img = new Bitmap(stream);
img.Save(#"c:\s\pic.png", System.Drawing.Imaging.ImageFormat.Png);
The last line there where you save the file, you can select the format.
So the answers by #mjwills and Cody was correct. I still thought to put the two methods I exactly needed:
public static Image BitmapToBytes(byte[] image, ImageFormat pFormat)
{
var imageObject = new Bitmap(new MemoryStream(image));
var stream = new MemoryStream();
imageObject.Save(stream, pFormat);
return new Bitmap(stream);
}
Also:
public static byte[] ImgToByteArray(Image img)
{
using (MemoryStream mStream = new MemoryStream())
{
img.Save(mStream, img.RawFormat);
return mStream.ToArray();
}
}
Cheers everyone.

c# Load Image from ftp server directly into picture box without downloading

I have looked every where for my answer but couldn't find the right solution.Tried many solutions provided but still can't get it through.I uploaded an image in ftp server and i want it to get displayed into picture box in windows form without downloading it into local machine. Is it possible?
Please include complete code for the solution......
Here is a complete code: If any body needs.Make sure the image isn't large!!
public byte [] GetImgByte (string ftpFilePath)
{
WebClient ftpClient = new WebClient();
ftpClient.Credentials = new NetworkCredential(ftpUsername,ftpPassword);
byte[] imageByte = ftpClient.DownloadData(ftpFilePath);
return imageByte;
}
public static Bitmap ByteToImage(byte[] blob)
{
MemoryStream mStream = new MemoryStream();
byte[] pData = blob;
mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
Bitmap bm = new Bitmap(mStream, false);
mStream.Dispose();
return bm;
}
You can use DownloadData to get a byte array and load that into the picturebox - see Download file directly to memory and How to put image in a picture box from a byte[] in C#

How to convert file .DEX (Dexis image file) to jpg image format using c#

I writing a program that it can read .Dex file (Dexis X-ray image file) and convert it to jpg image. but i can't find out how to decode it.
i use this code to read and convert but the image is corrupt.
Read Image:
public static byte[] ImageToByte(string IMG_PATH)
{
byte[] img = null;
try
{
FileStream IM_STREAM = new FileStream(IMG_PATH, FileMode.Open, FileAccess.Read);
BinaryReader IM_BNR = new BinaryReader(IM_STREAM);
img = IM_BNR.ReadBytes((int)IM_STREAM.Length);
IM_BNR.Close();
IM_STREAM.Close();
}
catch (Exception ex)
{
StreamWriter swr = new StreamWriter("LOG.txt", true, Encoding.UTF8);
swr.WriteLine(ex.Message);
swr.Close();
}
return img;
}
Convert Image:
public static void SaveToFile(string path, byte[] b)
{
MemoryStream ms = new MemoryStream(b);
Image img = Image.FromStream(ms);
img.Save(path + "\\exam.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
img.Dispose();
}
The image before convert (.Dex, size 800x600px) -> after convert (.jpg, size 80x60). plz help me.
.dex is a semi-proprietary format. Using standard image decoding libraries the result you are seeing is the best you can do. If you are looking for a paid solution I know of a team that is currently working on a web services API for this but it hasn't been released publicly yet. If you'd be interested in being an early tester I can put you in touch.

Displaying image in ASP.NET C#

I am converting Base64 code to image and I am using following way to save and display that image.
var kpin = Base64ToImage(TextBox1.Text);
kpin.Save(#"e:\myim.png");
Image1.ImageUrl = #"e:\myim.png";
and class is
public Image Base64ToImage(string base64String)
{
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0,
imageBytes.Length);
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
and this process working fine but I need an image not to be saved in hard disk. How to display this image directly without saving to hard disk and retrieving back.
Instead of setting the Image1.ImageURL to the path of your image, you can instead do one of several things:
Use an img tag with the Base64 data in it directly - http://www.sweeting.org/mark/blog/2005/07/12/base64-encoded-images-embedded-in-html
Not all browsers support this.
Create an Action or Webform (depending on whether you're using ASP.NET MVC or not) that takes as input whatever you need to either retrieve or generate the Base64 encoded data, and then set the response headers to serve the correct content type (image/png or something) and write the image directly to the Response.OutputStream (or use ContentResult in ASP.NET MVC). Tons of examples via Stackoverflow on how to do either.
-M
Don't bother with the image object at all, and just do it direct:
public void Base64ToResponse(string base64String)
{
Response.ContentType = "text/png"; //or whatever...
byte[] imageBytes = Convert.FromBase64String(base64String);
Response.OutputStream(imageBytes, 0, imageBytes.Length);
}

Save image to Sql Server image to image field using C# and Subsonic

In an ASP.NET app (using framework 2.0), and the ORM for this application is Subsonic 2.2, which I'm working with for the first time.
I have an image which I've retrieved from a URL, and I have an HttpWebresponse object, which I've converted to an image, like this:
string uri = ...;
HttpWebRequest lxRequest = (HttpWebRequest) WebRequest.Create(uri);
HttpWebResponse lxResponse = (HttpWebResponse) lxRequest.GetResponse();
System.Drawing.Image image = System.Drawing.Image.FromStream(lxResponse.GetResponseStream());
In Sql Server, I've created an Image column (could be any other binary format, that's not a problem), and I'd like to write my image into it. I'd also like to know how to read from the column, so as to test that the image has been correctly saved.
Any help appreciated. Thanks.
For a image column, Subsonic creates a byte[] property.
Therefore, you'll have to transform your data (System.Drawing.Image in this case) to and from a byte array.
Other sites have this covered, shamelessly copied:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
Have looked at Read and write SQL image data, this might help you

Categories