I'm having a problem when fetching an image using Image.GetInstance. The photo is in portrait but when I did the Image.GetInstance(), my image is rotated.
string imagepath = AttachmentURL + answer.Attachment.AzureFileName;
Image image = Image.GetInstance(imagepath);
My image:
https://zensyd.blob.core.windows.net/fileattachments/f9ab6a11-13c1-421a-abdd-4b4a6d701033.jpg
This means 2016 width and 1512 height.
I found this answer regarding image orientation rotation via exif but I need to convert the uri first to a memory stream. I then found this solution that converts URI to a MemoryStream. Then MemoryStream to System.Drawing Image.
Here's my solution
BaseColor color = null;
string imagepath = AttachmentURL + answer.Attachment.AzureFileName;
var imgStream = GetStreamFromUrl(imagepath);
var tempImg = System.Drawing.Image.FromStream(imgStream);
ExifRotate(tempImg);
Image image = Image.GetInstance(tempImg, color);
GetStreamFromUrl is balexandre's answer here
ExifRotate is saucecontrol's answer here.
I am using this code to get images from my webcam, I guess its Metricam library
Anyone knows how to get images without using picturebox?
WebCam camera = new WebCam();
camera.Connect();
pictureBox1.Image = camera.GetBitmap();
pictureBox1.Image.Save(#"c:\image1 " + ".Jpeg", ImageFormat.Jpeg);
pictureBox1.Image is Image type. It can handle saving on its own. It does handle saving when used from PictureBox too, notice how you call Save() method on Image property and not on pictureBox instance.
WebCam camera = new WebCam();
camera.Connect();
Image image = camera.GetBitmap();
image.Save(#"c:\image1 " + ".Jpeg", ImageFormat.Jpeg);
I am working on a form Image.aspx and I want to upload an image, resize it and display it in a new window.
Here I am uploading the image,
UploadPath = Server.MapPath("../BrandPic/" + cmbImages.SelectedValue);
FileName = cmbImages.SelectedValue;
Bitmap OriginalBM = new Bitmap(UploadPath);
Here I am resizing it
int NewWidth = 1024;
int newHeight = 768;
Size newSize = new Size(NewWidth, newHeight);
Bitmap ResizedBM = new Bitmap(OriginalBM, newSize);
Here I am passing it
HttpContext.Current.Session["Pic"] = ResizedBM;
Here is my source code to open the image in new window
<a href='<%=Convert.ToString(HttpContext.Current.Session["Pic"])%>' rel="lightbox" >
But there is an error occured. Check the URL.
So any idea to solve this issue ???
ResizedBM is a Bitmap variable, not a path to a file that can be referenced in your html. To do what you want you'll likely have to save it out as a file and then reference the file that you saved in your href.
you could have a generic handler like resizedImage.ashx that you could pass the image id to. Then your link would be <a href='resizedImage.ashx?id=[your image id]' rel='lightbox'>link text</a>. That generic handler would resize the image and output the image to the browser
more info at MVC3 generic handler (.ashx) for Images resizing (Need clean URL)
I have a grid of Images, I click one, I search for this image on that grid "2D array of Images"
then I want to check if this image is the one I need,
the images in the grid are:
BitmapImage img = new BitmapImage(new Uri("/Mines1.2;component/Images/new.png", UriKind.Relative));
Image temp = new Image();
temp.Height = 50;
temp.Width = 40;
temp.Source = img;
temp.Name = i.ToString() + j.ToString();
temp.MouseLeftButtonDown += new MouseButtonEventHandler(this.explore);
explore is the function I want to go when click on the image.
I want to compare now the image temp with another one, I do this with it's resource uri
if(temp.source.equals(new BitmapImage(new Uri("/Mines1.2;component/Images/new.png", UriKind.Relative))))
but this does not work!
I want to check if two images are the same or not using the uri or source or anything but of course I'm not going to check each pixel.
you could create a hash of the file and then compare whether the two hashes are the same.
I'm able to save the captured image from a barcode scanner using this code:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.DefaultExt = ".jpg";
dlg.Filter = "JPEG Images (.jpg)|*.jpg|All files (*.*)|*.*";
if (dlg.ShowDialog() == true)
{
using (FileStream file = File.OpenWrite(dlg.FileName))
{
file.Write(e.ImageBuffer, 0, e.ImageSize);
}
}
However, I would like to display the captured image using WPF but I get a distorted image.
private void _barcodeScannerInstance_SavePhotoEvent(object sender, ImageEventArgs e)
{
SetBitmap(e.ImageBuffer, 350, 263, 96);
}
private void SetBitmap(byte[] image, int width, int height, int dpi)
{
MainWindow.Instance.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate()
{
BitmapSource bitmapSource = BitmapSource.Create(
width, height, (double)dpi, (double)dpi, PixelFormats.Bgr24, null, image, ((width * 24 + 31) & ~31) >> 3);
HwModeScreen.BarcodeImageCanvas.Children.Clear();
Image myImage = new Image();
myImage.Width = HwModeScreen.BarcodeImageCanvas.ActualWidth;
myImage.Height = HwModeScreen.BarcodeImageCanvas.ActualHeight;
myImage.Stretch = Stretch.Fill;
myImage.Source = bitmapSource;
HwModeScreen.BarcodeImageCanvas.Children.Add(myImage);
});
Here is the image I see. It should be a black and white picture of a kleenex box.
Here is the saved jpg file:
did you mix up width and height? are you sure your dpi value is correct?
I suspect the whole problem is this line:
BitmapSource bitmapSource = BitmapSource.Create(
width, height, (double)dpi, (double)dpi, PixelFormats.Bgr24, null, image, ((width * 24 + 31) & ~31) >> 3)
What I would do to debug the issue is to write out the image to file and confirm all the inputs. Use photoshop, paint.net, file properties...
Are you sure you are working with bitmap format?
Are you sure you are working with 24bits per pixel?
Are you sure you have height and width correct, and you are feeding the values into the correct argument
What is this line all about, and why are you doing it? I am slightly suspicious.
((width * 24 + 31) & ~31) >> 3)
Basically, the way I look at this is that you are feeding the bitmap library a stream of bits... it doesn't know what the bits are but it will attempt to create the image from the information you give it: bits per pixel, size, etc. If you give it incorrect information, it will create a corrupted image as you have shown.
I am slightly suspicious that the problem is not with width and height; even if you mix those two values up-- I think you would get at least part of the first row of pixels to be rendered correctly. I see static / noise / snow, which tells me that there is something about the way the stream of bits was interpreted-- it is rendered as random blacks and whites.
Another thing: in your screen cap, I see color. this is another hint that there is something incorrect about your assumptions about the image. The values should probably 1 to 256 ( 8 bits per pixel I think? ) I would try creating a 8 bit per pixel black and white bitmap. Somehow the library thinks this is a color image.
I just noticed that you are assuming jpeg. jpeg is a lossy format-- I would have assumed that you would end up with a bitmap or tiff image. double check that you are indeed getting back a jpeg image (check the barcode api documentation)
The JPEG compression algorithm is quite unsuitable for the kind of image you are capturing. It works well for photos, it behaves poorly on images containing fine lines. The slight artifacts the compression produces makes it a lot harder to properly scan the barcode.
You don't see the Kleenex box because you are writing the raw image bytes. You need to use an image encoder. I recommend you use the PngBitmapEncoder class. GifBitmapEncoder should work too since you don't need a lot of colors, it makes smaller files. A code snippet that shows how to use an encoder is available here.
this is likely distorting it
myImage.Stretch = Stretch.Fill;
I used a jpeg decoder to fix the problem.
private void SetBitmap(byte[] image, int width, int height, int dpi)
{
MainWindow.Instance.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate()
{
BMemoryStream ms = new MemoryStream(image);
JpegBitmapDecoder decoder = new JpegBitmapDecoder(ms, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
HwModeScreen.BarcodeImageCanvas.Children.Clear();
Image myImage = new Image();
myImage.Width = HwModeScreen.BarcodeImageCanvas.ActualWidth;
myImage.Height = HwModeScreen.BarcodeImageCanvas.ActualHeight;
myImage.Stretch = Stretch.Fill;
myImage.Source = bitmapSource;
HwModeScreen.BarcodeImageCanvas.Children.Add(myImage);
});