I have an array of string from which I need to Draw an image in Windows forms.
I've done the following two steps:
converted the string array into memory stream.
and used the following code:
Image image = Image.FromStream(memory stream);
But it always returns null.
You can get image from byte array:
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
Be aware with formats. For example, Silverlight and WPF support only JPEG or PNG. Otherwise will get an exception.
Related
I am trying to raise the contrast of an image in Xamarin.forms using this algorithm. My code gives me in image in the form of a System.IO.Stream, which I want to convert to a Bitmap/LockBitmap to use the algorithm.
I tried code such as using System.Drawing, which didn't work. The error said "the type or namespace bitmap could not be found." Then, I tried using Android.Graphics, which fixes that error, but I don't know if it will work with a Xamarin.forms app that needs to run on Android and iOS.
Even if that does work, how do I actually convert a System.IO.Stream into a Bitmap? Is there another algorithm that doesn't require bitmaps?
Edit: I think I can use BitmapFactory to convert the stream into a bitmap.
you could use DependencyService to convert System.IO.Stream into Bitmap ,after raise the contrast of an image ,return the new stream,and show the Image in forms page.
like:
create a interface IRaiseImage.cs:
public interface IRaiseImage
{
Stream RaiseImage(Stream stream);
}
then in Droid.project,creat AndroidRaiseImage.cs:
[assembly: Dependency(typeof(AndroidRaiseImage))]
namespace App18.Droid
{
class AndroidRaiseImage : IRaiseImage
{
public Stream RaiseImage(Stream stream)
{
Bitmap bitmap = BitmapFactory.DecodeStream(stream);
//raise the bitmap contrast
...
// return the new stream
MemoryStream ms = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, ms);
return ms;
}
}
}
then you could set to the Image in your forms page:
MemoryStream memoryStream = new MemoryStream();//your original image stream
Image image = new Image();
image.Source = ImageSource.FromStream(() => DependencyService.Get<IRaiseImage>().RaiseImage(memoryStream));
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.
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#
My DB holds byte[] data for different images, png and bmp extensions work fine but strangely images with jpeg extension seem to be rotated 90deg anti-clockwise. I can only assume this is a problem with the encoder and decoder. The jpegs are encoded by an iPhone and decoded in a silverlight app using the following code:
using (MemoryStream ms = new MemoryStream(bImage, 0, bImage.Length))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms);
img.Source = bitmapImage;
tempList.Add(img);
}
For bmps I use the following:
ExtendedImage image = new ExtendedImage();
var bmp = new BmpDecoder();
var png = new PngEncoder();
try
{
bmp.Decode(image, new MemoryStream(bImage));
}
catch (NotSupportedException)
{
new MessageWindow("Incorrect image format", DisplayStrings.ErrorDisplay, MessageButtons.OK, MessageImage.Error).Show();
continue;
}
MemoryStream ms = new MemoryStream();
png.Encode(image, ms);
BitmapImage bi = new BitmapImage();
bi.SetSource(new MemoryStream(ms.ToArray()));
img.Source = bi;
tempList.Add(img);
Neither option will display the image in the correct orientation. Am I missing a specific jpeg decoder class?
EDIT
As it turns out, the iPhone was doing something strange to the byte array. As a result: rotating the image on the iPhone before saving apparently does nothing to the image on the iPhone visibly but uploads the correct orientation of image onto the server. Why this happens is anyone's guess but it is so.
Solved by rotating the image on the iPhone before uploading the byte array to the server. This saves the image with the correct orientation and apparently causes no visible difference to the original image on the iPhone.
I also want to display in a datagridview. I tried searching here and tried for hours to do it in all kinds of ways turning it to bitmap through drawtobitmap method and then turning it to a bite array and saving it to database
the database shows me 0X89 what does it mean?
And it doesn't show me the image on a datagridview
Can someone just give me a code that works and I'll improvise thank you very much.
For Saving
Bitmap bmp =new Bitmap(panel1.Width,panel1.Height);
panel1.DrawToBitmap(bmp, panel1.Bounds);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] result = new byte[ms.Length];
ms.Seek(0,System.IO.SeekOrigin.Begin);
ms.Read(result, 0, result.Length);
and save result to your sqlserver table
and to Convert Byte array to image use this
public static Bitmap ConvertBinaryDataToImage(byte[] buffer)
{
System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
Bitmap bmap = new Bitmap(ms);
return bmap;
}