public static Bitmap GetThumbnail(Bitmap bmpImage, Size size) { double width = bmpImage.Width; double height = bmpImage.Height; Double WidthToHeightAspect = (double)bmpImage.Width / (double)bmpImage.Height; Double xFactor = (double)size.Width / width; Double yFactor = (double)size.Height / height; Size scaledSize = new Size(); if (xFactor < yFactor) { scaledSize.Width = size.Width; scaledSize.Height = Convert.ToInt32(size.Width / WidthToHeightAspect); } else { scaledSize.Width = Convert.ToInt32(size.Height * WidthToHeightAspect); scaledSize.Height = size.Height; } Bitmap scaledOriginal; Bitmap thumnailBmp = new Bitmap(size.Width, size.Height); // Compact framework can't resize bitmaps! // Using OpenNETCF instead using (MemoryStream ms = new MemoryStream()) { ImagingFactoryClass imageFactory = new ImagingFactoryClass(); IImage image; IBitmapImage iBitmap; ImageInfo info; bmpImage.Save(ms, ImageFormat.Png); imageFactory.CreateImageFromStream(new StreamOnFile(ms), out image); image.GetImageInfo(out info); imageFactory.CreateBitmapFromImage(image, (uint)scaledSize.Width, (uint)scaledSize.Height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, out iBitmap); //Set up the scaled bitmap to be copied scaledOriginal = ImageUtils.IBitmapImageToBitmap(iBitmap); for (int y = 0; y < thumnailBmp.Height; y++) { for (int x = 0; x < thumnailBmp.Width; x++) { if ((x < scaledOriginal.Width) && (y < scaledOriginal.Height)) { thumnailBmp.SetPixel(x, y, scaledOriginal.GetPixel(x, y)); } else { thumnailBmp.SetPixel(x, y, Color.Transparent); } } } } return thumnailBmp; }
private void mnuLoadImaging_Click(object sender, EventArgs e) { SelectPictureDialog dlg = new SelectPictureDialog(); if (dlg.ShowDialog() == DialogResult.OK) { try { ImagingFactory factory = new ImagingFactoryClass(); IImage img; factory.CreateImageFromFile(dlg.FileName, out img); IBitmapImage imgB; factory.CreateBitmapFromImage(img, (uint)pbImage.Width, (uint)pbImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb, InterpolationHint.InterpolationHintDefault, out imgB); pbImage.Image = ImageUtils.IBitmapImageToBitmap(imgB); } catch (OutOfMemoryException) { MessageBox.Show("Out of memory"); } } }
// Loads image from a specified url private void LoadImage(string url) { HttpWebRequest rq = HttpWebRequest.Create(url) as HttpWebRequest; HttpWebResponse rsp = null; try { // Request data from the url rsp = rq.GetResponse() as HttpWebResponse; Stream st = rsp.GetResponseStream(); // Read all the image data to memory // If we run out of memory, so be it. ms = new MemoryStream(); byte[] buffer = new byte[1024]; int cb = 0; while ((cb = st.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, cb); } st.Close(); rsp.Close(); // Use ImageFactory class to load image _stream = new StreamOnFile(ms); if (_stream != null) { IImagingFactory factory = new ImagingFactoryClass(); factory.CreateImageFromStream(_stream, out _image); } } catch (WebException wex) { if (wex.Response != null) { wex.Response.Close(); } throw; } catch { MessageBox.Show("Unable to load image"); } finally { if (rsp != null) { rsp.Close(); } } }
private void loadImage(String path, int maxRes) { try { if (pictureBoxImages.Image != null) { pictureBoxImages.Image.Dispose(); pictureBoxImages.Image = null; } ImagingFactory factory = new ImagingFactoryClass(); IImage img; ImageInfo inf = new ImageInfo(); factory.CreateImageFromFile(path, out img); img.GetImageInfo(out inf); double ratio = (double)inf.Width / (double)inf.Height; uint x = 0; uint y = 0; if (inf.Width > inf.Height) { x = Math.Min(inf.Width, (uint)maxRes); y = (uint)Math.Floor(x / ratio); } else { y = Math.Min(inf.Height, (uint)maxRes); x = (uint)Math.Floor(y * ratio); } IBitmapImage imgB; factory.CreateBitmapFromImage(img, x, y, System.Drawing.Imaging.PixelFormat.Format24bppRgb, InterpolationHint.InterpolationHintDefault, out imgB); Size s = new Size((int)x, (int)y); pictureBoxImages.Size = s; pictureBoxImages.Image = ImageUtils.IBitmapImageToBitmap(imgB); } catch (OutOfMemoryException) { MessageBox.Show("Out of memory"); } }
private void menuItem1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { StreamOnFile st = new StreamOnFile(openFileDialog1.FileName); IImageDecoder decoder = null; ImagingFactory factory = new ImagingFactoryClass(); factory.CreateImageDecoder(st, DecoderInitFlag.DecoderInitFlagNone, out decoder); ImageProperty[] props = ImageUtils.GetAllProperties(decoder); foreach (ImageProperty prop in props) { //For Specific tags see ImageTag enum textBox1.Text = prop.Id.ToString() + ": " + GetValue(prop) + "\r\n" + textBox1.Text; } decoder.TerminateDecoder(); } }
public static IBitmapImage CreateThumbnail(Stream stream, Size size) { IBitmapImage imageBitmap; ImageInfo ii; IImage image; ImagingFactory factory = new ImagingFactoryClass(); factory.CreateImageFromStream( new StreamOnFile(stream), out image); image.GetImageInfo(out ii); factory.CreateBitmapFromImage( image, (uint)size.Width, (uint)size.Height, ii.PixelFormat, InterpolationHint.InterpolationHintDefault, out imageBitmap); return imageBitmap; }
public static bool GetImageSize(IImage inputIImage, out Size resultSize) { resultSize = new Size(0, 0); IImagingFactory newIImagingFactory = new ImagingFactoryClass(); ImageInfo newImageInfo; try { inputIImage.GetImageInfo(out newImageInfo); resultSize = new Size((int)newImageInfo.Width, (int)newImageInfo.Height); } catch { return(false); } return(true); }
static public IBitmapImage CreateGrayScaleImage(Stream stream) { ImagingFactory factory = new ImagingFactoryClass(); IImage image; factory.CreateImageFromStream(new StreamOnFile(stream), out image); ImageInfo ii; image.GetImageInfo(out ii); image.SetImageFlags((ImageFlags)((int)ii.Flags & (int)ImageFlags.ImageFlagsColorSpaceGRAY)); IBitmapImage imageBitmap = null; try { factory.CreateBitmapFromImage(image, (uint)ii.Width, (uint)ii.Height, ii.PixelFormat, InterpolationHint.InterpolationHintDefault, out imageBitmap); } catch { } return(imageBitmap); }
public static bool GetImageSize(IImage inputIImage, out Size resultSize) { resultSize = new Size(0, 0); IImagingFactory newIImagingFactory = new ImagingFactoryClass(); ImageInfo newImageInfo; try { inputIImage.GetImageInfo(out newImageInfo); resultSize = new Size((int)newImageInfo.Width, (int)newImageInfo.Height); } catch { return false; } return true; }
private void Form1_Load(object sender, EventArgs e) { IImage fullSizeImage; IImagingFactory factory = new ImagingFactoryClass(); factory.CreateImageFromFile(@"\My Documents\My Pictures\luminous opera house.jpg", out fullSizeImage); fullSizeImage.GetThumbnail(240, 320, out smallImage); using (Graphics gfx = Graphics.FromImage(bmp)) { IntPtr hdc = gfx.GetHdc(); try { smallImage.Draw(hdc, new Rectangle(0, 0, 240, 320), IntPtr.Zero); } finally { gfx.ReleaseHdc(hdc); } } }
public static bool ShrinkToMaxLinearSize(string fileName, uint maxLinearSize) { IImagingFactory newIImagingFactory = new ImagingFactoryClass(); IImage newIImage = null; IImage newIImageThumb = null; ImageInfo newImageInfo; uint imageThumbWidth = 0; uint imageThumbHeight = 0; if (!File.Exists(fileName)) { return(false); } try { using (var newStreamOnFile = new StreamOnFile(fileName)) { newIImagingFactory.CreateImageFromStream(newStreamOnFile, out newIImage); newIImage.GetImageInfo(out newImageInfo); uint imageWidth = newImageInfo.Width; uint imageHeight = newImageInfo.Height; if (imageWidth > maxLinearSize || imageHeight > maxLinearSize) { float wa = (float)imageWidth / (float)maxLinearSize; float ha = (float)imageHeight / (float)maxLinearSize; float a = (float)imageWidth / (float)imageHeight; if (wa > ha) { imageThumbWidth = maxLinearSize; imageThumbHeight = (uint)(maxLinearSize / a); } else { imageThumbHeight = maxLinearSize; imageThumbWidth = (uint)(maxLinearSize * a); } newIImage.GetThumbnail(imageThumbWidth, imageThumbHeight, out newIImageThumb); Marshal.FinalReleaseComObject(newIImage); } } } catch { return(false); } try { File.Delete(fileName); IBitmapImage newIBitmapImage; newIImagingFactory.CreateBitmapFromImage(newIImageThumb, imageThumbWidth, imageThumbHeight, newImageInfo.PixelFormat, InterpolationHint.InterpolationHintBilinear, out newIBitmapImage); Bitmap newBitmap = ImageUtils.IBitmapImageToBitmap(newIBitmapImage); newBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); } catch { return(false); } return(true); }
public static bool ShrinkToMaxLinearSize(string fileName, uint maxLinearSize) { IImagingFactory newIImagingFactory = new ImagingFactoryClass(); IImage newIImage = null; IImage newIImageThumb = null; ImageInfo newImageInfo; uint imageThumbWidth = 0; uint imageThumbHeight = 0; if (!File.Exists(fileName)) { return false; } try { using (var newStreamOnFile = new StreamOnFile(fileName)) { newIImagingFactory.CreateImageFromStream(newStreamOnFile, out newIImage); newIImage.GetImageInfo(out newImageInfo); uint imageWidth = newImageInfo.Width; uint imageHeight = newImageInfo.Height; if (imageWidth > maxLinearSize || imageHeight > maxLinearSize) { float wa = (float)imageWidth / (float)maxLinearSize; float ha = (float)imageHeight / (float)maxLinearSize; float a = (float)imageWidth / (float)imageHeight; if (wa > ha) { imageThumbWidth = maxLinearSize; imageThumbHeight = (uint)(maxLinearSize / a); } else { imageThumbHeight = maxLinearSize; imageThumbWidth = (uint)(maxLinearSize * a); } newIImage.GetThumbnail(imageThumbWidth, imageThumbHeight, out newIImageThumb); Marshal.FinalReleaseComObject(newIImage); } } } catch { return false; } try { File.Delete(fileName); IBitmapImage newIBitmapImage; newIImagingFactory.CreateBitmapFromImage(newIImageThumb, imageThumbWidth, imageThumbHeight, newImageInfo.PixelFormat, InterpolationHint.InterpolationHintBilinear, out newIBitmapImage); Bitmap newBitmap = ImageUtils.IBitmapImageToBitmap(newIBitmapImage); newBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); } catch { return false; } return true; }
private void loadImage(String path, int maxRes) { try { if (pictureBoxMap.Image != null) { pictureBoxMap.Image.Dispose(); pictureBoxMap.Image = null; } ImagingFactory factory = new ImagingFactoryClass(); IImage img; ImageInfo inf = new ImageInfo(); factory.CreateImageFromFile(path, out img); img.GetImageInfo(out inf); double ratio = (double)inf.Width / (double)inf.Height; uint x=0; uint y=0; if (inf.Width > inf.Height) { x = Math.Min(inf.Width, (uint)maxRes); y = (uint)Math.Floor(x / ratio); } else { y = Math.Min(inf.Height, (uint)maxRes); x = (uint)Math.Floor(y * ratio); } IBitmapImage imgB; factory.CreateBitmapFromImage(img, x, y, System.Drawing.Imaging.PixelFormat.Format24bppRgb, InterpolationHint.InterpolationHintDefault, out imgB); Size s = new Size((int)x, (int)y); pictureBoxMap.Size = s; pictureBoxMap.Image = ImageUtils.IBitmapImageToBitmap(imgB); } catch (OutOfMemoryException) { MessageBox.Show("Out of memory"); } }