public static Bitmap CreateThumbnail(StreamOnFile sof, int width, int height) { IBitmapImage thumbnail; IImage image; ImageInfo info; GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); GetFactory().CreateBitmapFromImage(image, (uint)width, (uint)height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, out thumbnail); try { return ImageUtils.IBitmapImageToBitmap(thumbnail); } finally { Marshal.ReleaseComObject(thumbnail); } } finally { Marshal.ReleaseComObject(image); } }
public static Bitmap CreateThumbnail(StreamOnFile sof, int width, int height) { IBitmapImage thumbnail; IImage image; ImageInfo info; GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); GetFactory().CreateBitmapFromImage(image, (uint)width, (uint)height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, out thumbnail); try { return(ImageUtils.IBitmapImageToBitmap(thumbnail)); } finally { Marshal.ReleaseComObject(thumbnail); } } finally { Marshal.ReleaseComObject(image); } }
private static IImage GetImage(StreamOnFile stream) { IImagingFactory factory = null; try { factory = (IImagingFactory)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("327ABDA8-072B-11D3-9D7B-0000F81EF32E"))); if (factory != null) { IImage imagingImage; uint result = factory.CreateImageFromStream(stream, out imagingImage); if (result == BitmapProperties.S_OK) { return(imagingImage); } } } catch (COMException) { } catch (IOException) { } finally { if (factory != null) { Marshal.ReleaseComObject(factory); } } return(null); }
private void CreateObjects() { brSolid = new SolidBrushPlus(Color.CornflowerBlue); brHatch = new HatchBrush(HatchStyle.HatchStyle25Percent, Color.Black, Color.White); string bitmapPath = System.IO.Path.GetDirectoryName(GetType().Assembly.GetModules()[0].FullyQualifiedName); bitmapPath = System.IO.Path.Combine(bitmapPath, "brushPattern.bmp"); StreamOnFile sf = new StreamOnFile(bitmapPath); ImagePlus img = new ImagePlus(sf, false); brTexture = new TextureBrushPlus(img, WrapMode.WrapModeTile); brLinGrad = new LinearGradientBrush(new GpPointF(0, 0), new GpPointF(50, 50), Color.Black, Color.White); // Create rectangular path GraphicsPath path = new GraphicsPath(FillMode.FillModeAlternate); path.AddRectangle(new GpRectF(0, 0, ClientRectangle.Width, ClientRectangle.Height / 5)); // Create rectangular gradient brush // with red in center and black in the corners brPathGrad = new PathGradientBrush(path); brPathGrad.SetCenterColor(Color.Red); int count = 2; brPathGrad.SetSurroundColors(new Color[] { Color.Black, Color.Black }, ref count); }
public static bool saveAsJpg(string sFileIn, string sFileOut) { var filestream = System.IO.File.Open(sFileIn, System.IO.FileMode.Open); OpenNETCF.Drawing.Imaging.StreamOnFile sof; sof = new StreamOnFile(filestream); IImage image; ImageInfo info; System.Drawing.Bitmap _image; IBitmapImage thumbnail; bool bRet = false; GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); GetFactory().CreateBitmapFromImage(image, (uint)info.Width, (uint)info.Height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, out thumbnail); _image = ImageUtils.IBitmapImageToBitmap(thumbnail); _image.Save(sFileOut, ImageFormat.Jpeg); bRet = true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in saveAsJpg: " + ex.Message); bRet = false; } finally { Marshal.ReleaseComObject(image); } return(bRet); }
/// <summary> /// Читает угол поворота из EXIF /// </summary> /// <param name="file"></param> /// <param name="exifRotationAngle"></param> public static void ReadRotationAngleFromEXIF(string file, out RotationAngle exifRotationAngle) { exifRotationAngle = RotationAngle.Zero; using (StreamOnFile newStreamOnFile = new StreamOnFile(file)) { IImageDecoder newIImageDecoder; OpenNETCF.Drawing.Imaging.IImagingFactory newImagingFactory = new OpenNETCF.Drawing.Imaging.ImagingFactoryClass(); int r = newImagingFactory.CreateImageDecoder(newStreamOnFile, DecoderInitFlag.DecoderInitFlagNone, out newIImageDecoder); // Ниже используется своя функция обработки EXIF, так как на Philips попытка проверить // jpeg из-за некорректных данных рушится в COM. Это случается из-за параметра DecoderInitFlag.DecoderInitFlagNone // ImageProperty[] items = ImageUtils.GetAllProperties(newIImageDecoder); ImageProperty[] items = GetAllPropertiesInternal(newIImageDecoder); if (items.Length == 0) { newImagingFactory.CreateImageDecoder(newStreamOnFile, DecoderInitFlag.DecoderInitFlagBuiltIn1st, out newIImageDecoder); items = GetAllPropertiesInternal(newIImageDecoder); } foreach (ImageProperty item in items) { if (item.Id == ImageTag.TAG_ORIENTATION) { try { switch (Convert.ToInt16(item.GetValue())) { case 3: exifRotationAngle = RotationAngle.Clockwise180; break; case 6: exifRotationAngle = RotationAngle.Clockwise90; break; case 8: exifRotationAngle = RotationAngle.Clockwise270; break; default: throw new Exception(); break; } } catch (Exception) { exifRotationAngle = RotationAngle.Zero; } } } } }
private void MainForm_Load(object sender, EventArgs e) { GpStatusPlus stat = NativeMethods.GdiplusStartup(out token, input, out output); string bitmapPath = System.IO.Path.GetDirectoryName(GetType().Assembly.GetModules()[0].FullyQualifiedName); bitmapPath = System.IO.Path.Combine(bitmapPath, "test.jpg"); StreamOnFile sf = new StreamOnFile(bitmapPath); bmp = new BitmapPlus(sf); penWrite = new PenPlus(Color.Blue, 3); path = new GraphicsPath(FillMode.FillModeAlternate); }
// 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 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 Size GetRawImageSize(StreamOnFile sof) { IImage image; ImageInfo info; GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); return(new Size((int)info.Width, (int)info.Height)); } finally { Marshal.ReleaseComObject(image); } }
public static Bitmap CreateClip(StreamOnFile sof, int x, int y, int width, int height) { IBitmapImage original = null; IImage image = null; ImageInfo info; GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); GetFactory().CreateBitmapFromImage(image, info.Width, info.Height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, out original); try { var ops = (IBasicBitmapOps)original; IBitmapImage clip = null; try { var rect = new RECT(x, y, x + width, y + height); ops.Clone(rect, out clip, true); return ImageUtils.IBitmapImageToBitmap(clip); } finally { Marshal.ReleaseComObject(clip); } } finally { Marshal.ReleaseComObject(original); } } finally { Marshal.ReleaseComObject(image); } }
public static Bitmap CreateClip(StreamOnFile sof, int x, int y, int width, int height) { IBitmapImage original = null; IImage image = null; ImageInfo info; GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); GetFactory().CreateBitmapFromImage(image, info.Width, info.Height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, out original); try { var ops = (IBasicBitmapOps)original; IBitmapImage clip = null; try { var rect = new RECT(x, y, x + width, y + height); ops.Clone(rect, out clip, true); return(ImageUtils.IBitmapImageToBitmap(clip)); } finally { Marshal.ReleaseComObject(clip); } } finally { Marshal.ReleaseComObject(original); } } finally { Marshal.ReleaseComObject(image); } }
public static bool saveAsJpg(string sFileIn, string sFileOut){ var filestream = System.IO.File.Open(sFileIn, System.IO.FileMode.Open); OpenNETCF.Drawing.Imaging.StreamOnFile sof; sof = new StreamOnFile(filestream); IImage image; ImageInfo info; System.Drawing.Bitmap _image; IBitmapImage thumbnail; bool bRet = false; GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); GetFactory().CreateBitmapFromImage(image, (uint)info.Width, (uint)info.Height, info.PixelFormat, InterpolationHint.InterpolationHintDefault, out thumbnail); _image = ImageUtils.IBitmapImageToBitmap(thumbnail); _image.Save(sFileOut, ImageFormat.Jpeg); bRet = true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in saveAsJpg: " + ex.Message); bRet = false; } finally { Marshal.ReleaseComObject(image); } return bRet; }
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); }
void doccap_Capture(object sender, DocumentCaptureEventArgs DocCaptureEventArgs) { if (DocCaptureEventArgs.DocCapInProcess) { this.statusBar1.Text = "DocCap in process, please wait..."; } //display captured doucment image file if (DocCaptureEventArgs.DocCapFile.Length > 0) { string filePath = DocCaptureEventArgs.DocCapFile.ToString(); this.statusBar1.Text = "File Path: " + filePath; if (File.Exists(filePath)) { if (_Bitmap != null) _Bitmap.Dispose(); //create a scaled down image file var stream = File.Open(filePath, FileMode.Open); StreamOnFile _stream = new StreamOnFile(stream); _Bitmap = ImageHelper.getScaledBitmap(_stream, 50);// new Bitmap(filePath); _stream.Dispose(); _Bitmap.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg); //show image imagePanel1.loadImage(filePath); //this.pictureBox1.Image = (Image)_Bitmap; _filePath = filePath; if(_sendMail.bIsValidAccount) btnSend.Enabled = true; else btnSend.Enabled = false; } } }
/// <summary> /// Gets the ImageType of the given file /// </summary> /// <param name="fileName">Path of the file to get the info of</param> /// <returns>ImageType of the given file</returns> public static ImageType GetImageType(string fileName) { IImage imagingImage = null; try { if (File.Exists(fileName)) { using (StreamOnFile fileStream = new StreamOnFile(fileName)) { imagingImage = BitmapProperties.GetImage(fileStream); if (imagingImage != null) { ImageInfo info = new ImageInfo(); uint ret = imagingImage.GetImageInfo(ref info); if (ret == BitmapProperties.S_OK) { if (info.RawDataFormat == BitmapProperties.ImageFormatBMP) { return(ImageType.BMP); } else if (info.RawDataFormat == BitmapProperties.ImageFormatEMF) { return(ImageType.EMF); } else if (info.RawDataFormat == BitmapProperties.ImageFormatEXIF) { return(ImageType.EXIF); } else if (info.RawDataFormat == BitmapProperties.ImageFormatGIF) { return(ImageType.GIF); } else if (info.RawDataFormat == BitmapProperties.ImageFormatIcon) { return(ImageType.Icon); } else if (info.RawDataFormat == BitmapProperties.ImageFormatJPEG) { return(ImageType.JPEG); } else if (info.RawDataFormat == BitmapProperties.ImageFormatMemoryBMP) { return(ImageType.MemoryBMP); } else if (info.RawDataFormat == BitmapProperties.ImageFormatPNG) { return(ImageType.PNG); } else if (info.RawDataFormat == BitmapProperties.ImageFormatTIFF) { return(ImageType.TIFF); } else if (info.RawDataFormat == BitmapProperties.ImageFormatWMF) { return(ImageType.WMF); } } } } } return(ImageType.Undefined); } finally { if (imagingImage != null) { Marshal.ReleaseComObject(imagingImage); } } }
public static bool saveScaledBitmap(StreamOnFile sof, int width, int height, string sNewFile) { bool bRet = true; try { Bitmap bmp = CreateThumbnail(sof, width, height); bmp.Save(sNewFile, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception in saveScaledBitmap(): " + ex.Message); bRet = false; } return bRet; }
public static Bitmap getScaledBitmap(StreamOnFile sof, int scalePercent) { IBitmapImage thumbnail; IImage image; ImageInfo info; decimal fScale = scalePercent / 100.0m; // do not remove the m specifier! GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); uint newWidth = (uint)(info.Width * fScale); uint newHeight = (uint)(info.Height * fScale); GetFactory().CreateBitmapFromImage(image, newWidth , newHeight, info.PixelFormat, InterpolationHint.InterpolationHintDefault, out thumbnail); try { return ImageUtils.IBitmapImageToBitmap(thumbnail); } finally { Marshal.ReleaseComObject(thumbnail); } } finally { Marshal.ReleaseComObject(image); } }
public static Size GetRawImageSize(StreamOnFile sof) { IImage image; ImageInfo info; GetFactory().CreateImageFromStream(sof, out image); try { image.GetImageInfo(out info); return new Size((int)info.Width, (int)info.Height); } finally { Marshal.ReleaseComObject(image); } }
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; }