How to correctly read an image file? - c#

I tried using FileStream to read the image file and it succeeded reading it, but it outputs this error message
"Parameter is not valid".
public Bitmap streamimage(string Fname)
{
Bitmap bm;
using (FileStream stream = new FileStream(Fname, FileMode.Open, FileAccess.Read))
{
bm = (Bitmap)Image.FromStream(stream);
stream.Close();
return bm;
}
}

Use
Image I = Image.FromFile("FilePath");
And use that image
Bitmap bm= new Bitmap(I);
Or
Bitmap bm= new Bitmap("FilePath");
And you can edit your code like this
public Bitmap streamimage(string Fname)
{
Bitmap bm;
FileStream stream = new FileStream(Fname, FileMode.Open, FileAccess.Read);
bm = (Bitmap)Image.FromStream(stream);
return bm;
}

When opening from a stream, the stream must remain open.
I would suggest that you use the contructor of the Bitmap that takes the file path as a parameter.
return new Bitmap(Fname);

Related

JpegBitmapEncoder cannot access closed stream

I have a BitmapSource loaded from a byte array this way:
BitmapSource bitmap = null;
using (var stream = new MemoryStream(bytes))
{
var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
bitmap = decoder.Frames[0];
stream.Flush();
}
return bitmap;
When I'm trying to write this as an JPEG to disk, it only throws an error:
Any ideas here? Other ways of writing the image to disk?

Save image from picturebox default image (from resources)

how to save this image
into sqlserver, i usually save it from file name but i don't know how to save from resources
Try this:
public byte[] WinImage=new byte[0];
MemoryStream stream = new MemoryStream();
PictureBox.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
WinImage=stream.ToArray();
And save it to the table as varbinary(max).
To open the image from database:
MemoryStream stream = new MemoryStream(byte[] WinImage);
Image RetImage = Image.FromStream(stream);
PictureBox.Image = RetImage;
You can convert image to filestream like this,
FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);
data = new byte[fs.Length];
fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
Then you can save the image as byte[] array to your database table where the field datatype is Image.

convert binary to bitmap using memory stream

Hi I wanna convert binary array to bitmap and show image in a picturebox. I wrote the following code but I got exception that says that the parameter is not valid .
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);
mStream.Dispose();
return bm;
}
It really depends on what is in blob. Is it a valid bitmap format (like PNG, BMP, GIF, etc?). If it is raw byte information about the pixels in the bitmap, you can not do it like that.
It may help to rewind the stream to the beginning using mStream.Seek(0, SeekOrigin.Begin) before the line Bitmap bm = new Bitmap(mStream);.
public static Bitmap ByteToImage(byte[] blob)
{
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Seek(0, SeekOrigin.Begin);
Bitmap bm = new Bitmap(mStream);
return bm;
}
}
Don't dispose of the MemoryStream. It now belongs to the image object and will be disposed when you dispose the image.
Also consider doing it like this
var ms = new MemoryStream(blob);
var img = Image.FromStream(ms);
.....
img.Dispose(); //once you are done with the image.
System.IO.MemoryStream mStrm = new System.IO.MemoryStream(your byte array);
Image im = Image.FromStream(mStrm);
im.Save("image.bmp");
Try this. If you still get any error or exception; please post your bytes which you are trying to convert to image. There should be problem in your image stream....

How to crop a tiff image in asp.net

I was looking for a routine by which I can crop tiff image and I got it but it gives many error. Here is the routine:
Bitmap comments = null;
string input = "somepath";
// Open a Stream and decode a TIFF image
using (Stream imageStreamSource = new FileStream(input, FileMode.Open, FileAccess.Read, FileShare.Read))
{
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
using (Bitmap b = BitmapFromSource(bitmapSource))
{
Rectangle cropRect = new Rectangle(169, 1092, 567, 200);
comments = new Bitmap(cropRect.Width, cropRect.Height);
//first cropping
using (Graphics g = Graphics.FromImage(comments))
{
g.DrawImage(b, new Rectangle(0, 0, comments.Width, comments.Height),
cropRect,
GraphicsUnit.Pixel);
}
}
}
When I try to compile it, I get an error. I tried adding references to many assemblies searching google but couldn't resolve it. I got this code from this url:
http://snipplr.com/view/63053/
I am looking for advice.
TiffBitmapDecoder class is from Presentation.Core in another words it is from WPF.
BitmapFromSource isn't method of any .net framework class. You can convert BitmapSource to Bitmap using this code.
private Bitmap BitmapFromSource(BitmapSource bitmapsource)
{
Bitmap bitmap;
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapsource));
encoder.Save(outStream);
bitmap = new Bitmap(outStream);
}
return bitmap;
}

C# Load JPG file, extract BitmapImage

I am trying to extract a BitmapImage from a JPG. This is the code I have:
FileStream fIn = new FileStream(sourceFileName, FileMode.Open); // source JPG
Bitmap dImg = new Bitmap(fIn);
MemoryStream ms = new MemoryStream();
dImg.Save(ms, ImageFormat.Jpeg);
image = new BitmapImage();
image.BeginInit();
image.StreamSource = new MemoryStream(ms.ToArray());
image.EndInit();
ms.Close();
image comes back with a 0 × 0 image, which of course means it didn't work. How do I do this?
Try this:
public void Load(string fileName)
{
using(Stream BitmapStream = System.IO.File.Open(fileName,System.IO.FileMode.Open ))
{
Image img = Image.FromStream(BitmapStream);
mBitmap=new Bitmap(img);
//...do whatever
}
}
Or you can just do this (source):
Bitmap myBmp = Bitmap.FromFile("path here");

Categories