public System.Windows.Media.ImageSource testEmgu(System.Windows.Interop.InteropBitmap interopBitmap) { Image <Bgr, Byte> img = new Image <Bgr, byte>(new System.Drawing.Bitmap(ToBitmap(interopBitmap))); //Image<Bgr, Byte> img = new Image<Bgr, byte>(new System.Drawing.Bitmap((System.Drawing.Bitmap)interopBitmap)); return(ToBitmapSource(img)); }
public static byte[] GetBytesFromInteropBitmap(System.Windows.Interop.InteropBitmap ibmp) { BitmapSource bmpSource = ibmp as BitmapSource; using (MemoryStream ms = new MemoryStream()) { BitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(bmpSource)); encoder.Save(ms); ms.Seek(0, SeekOrigin.Begin); return(ms.GetBuffer()); } }
private void InternalAllocate(int width, int height) { width = Math.Max(width, 1); height = Math.Max(height, 1); _width = width; _height = height; uint numBytesToAllocate = (uint)(width * height * BytesPerPixel); _section = CreateFileMapping(INVALID_HANDLE_VALUE, IntPtr.Zero, PAGE_READWRITE, 0, numBytesToAllocate, null); if (_section == IntPtr.Zero) { throw new InvalidOperationException(string.Format("Unable to create file mapping for {0} x {1} pixel. ActiveInstances: {2}, NumberOfBytesAllocated={3} ", width, height, _activeInstances, _totalBytesAllocated)); } _map = MapViewOfFile(_section, FILE_MAP_ALL_ACCESS, 0, 0, numBytesToAllocate); if (IntPtr.Zero == _map) { System.GC.Collect(); _map = MapViewOfFile(_section, FILE_MAP_ALL_ACCESS, 0, 0, numBytesToAllocate); } if (_map == IntPtr.Zero) { CloseHandle(_section); _section = IntPtr.Zero; string exception = string.Format("Unable to create view of file for {0} x {1} pixel. ActiveInstances: {2}, NumberOfBytesAllocated={3} ", width, height, _activeInstances, _totalBytesAllocated); width = height = 0; throw new InvalidOperationException(exception); } _bmp = new System.Drawing.Bitmap(_width, _height, _width * BytesPerPixel, System.Drawing.Imaging.PixelFormat.Format32bppArgb, _map); GC.AddMemoryPressure(numBytesToAllocate); _totalBytesAllocated += numBytesToAllocate; ++_activeInstances; _interopBmp = (System.Windows.Interop.InteropBitmap)System.Windows.Interop.Imaging.CreateBitmapSourceFromMemorySection(_section, _width, _height, System.Windows.Media.PixelFormats.Bgra32, _width * BytesPerPixel, 0); }
private void InternalDeallocate(bool disposing) { if (disposing) { // free managed resources if (_bmp != null) { _bmp.Dispose(); _bmp = null; } if (_interopBmp != null) { _interopBmp = null; } } // now free all unmanaged resources if (IntPtr.Zero != _map) { UnmapViewOfFile(_map); _map = IntPtr.Zero; } if (IntPtr.Zero != _section) { CloseHandle(_section); _section = IntPtr.Zero; } var bytesToFree = _width * _height * BytesPerPixel; if (0 != bytesToFree) { GC.RemoveMemoryPressure(bytesToFree); _totalBytesAllocated -= bytesToFree; } --_activeInstances; _width = 0; _height = 0; }
protected void initialize() { if (_isBitmapInitialized || _isBitmapInvalid) { deallocate(); } var byteCount = (uint)(_width * _height * _bytesPerPixel); _mapFileHandle = NativeMethods.CreateFileMapping(new IntPtr(-1), IntPtr.Zero, PAGE_READWRITE, 0, byteCount, null); _mapViewPtr = NativeMethods.MapViewOfFile(_mapFileHandle, FILE_MAP_ALL_ACCESS, 0, 0, byteCount); _interopBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromMemorySection( _mapFileHandle, _width, _height, _pixelFormat, _width * _bytesPerPixel, 0) as System.Windows.Interop.InteropBitmap; _gdiGraphics = getGdiGraphics(_mapViewPtr); _isBitmapInitialized = true; _isBitmapInvalid = false; }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) { Dispatcher.BeginInvoke((Action)(() => { //foreach (FieldInfo type in typeof(DataFormats).GetFields()) //{ // if (Clipboard.GetDataObject().GetDataPresent(type.GetValue("") as string)) // { // Console.WriteLine(type.GetValue("") as string); // } //} try { transfareData IDataTranfare = new transfareData(); foreach (string _supported in supportedFormats) { if (!Clipboard.GetDataObject().GetDataPresent(_supported)) { continue; } if (_supported == DataFormats.Bitmap) { Image image = new Image(); System.Windows.Interop.InteropBitmap interopBitmap = (System.Windows.Interop.InteropBitmap)Clipboard.GetImage(); image.Source = interopBitmap; IDataTranfare.copyValue = image; BitmapFrame interopBitmap1 = CreateResizedImage(interopBitmap, Math.Min(interopBitmap.PixelWidth, 100), Math.Min(interopBitmap.PixelHeight, 100), 0); Image image2 = new Image(); image2.Source = interopBitmap1; IDataTranfare.contentValue = image2; Int32Rect rect = new Int32Rect(0, 0, interopBitmap1.PixelWidth, interopBitmap1.PixelHeight); int stride = interopBitmap1.PixelWidth * (interopBitmap1.Format.BitsPerPixel + 7) / 8; int arrayLength = stride * interopBitmap1.PixelHeight; int[] arr = new int[arrayLength]; interopBitmap1.CopyPixels(rect, arr, stride, 0); unchecked { int sum = 0; for (int i = 0; i < arr.Length; i++) { sum += arr[i]; } IDataTranfare.savedValue = sum / arr.Length; } } else if (_supported == DataFormats.Text) { IDataTranfare.copyValue = Clipboard.GetText().Replace("\n", "").Replace("\r", ""); IDataTranfare.savedValue = IDataTranfare.copyValue; IDataTranfare.contentValue = IDataTranfare.copyValue; } IDataTranfare.format = _supported; break; } if (OLD_IData.Contains(IDataTranfare.savedValue) || IDataTranfare.format == null) { return; } OLD_IData.Add(IDataTranfare.savedValue); ListBOX.Items.Insert(0, new itemElement(ListBOX, OLD_IData, IDataTranfare)); } catch (Exception err) { Console.WriteLine(err); } })); }
/// <summary> /// 提取图片 /// </summary> private List <ImagesDetailInfo> GetPicsFromExcel() { List <ImagesDetailInfo> result = new List <ImagesDetailInfo>(); try { var workBook = Globals.ThisAddIn.Application.ActiveWorkbook; var workSheet = (Worksheet)workBook.ActiveSheet; FileOperateHelper.DeleteFolder(savePathGetImage); if (!Directory.Exists(savePathGetImage)) { Directory.CreateDirectory(savePathGetImage); } for (int i = 1; i <= workSheet.Shapes.Count; i++) { var pic = workSheet.Shapes.Item(i); if (pic != null && pic.Type == Microsoft.Office.Core.MsoShapeType.msoPicture) { try { pic.Copy(); IDataObject ido = null; Dispatcher.Invoke(new System.Action(() => { try { ido = Clipboard.GetDataObject(); } catch { } })); if (ido != null && ido.GetDataPresent(DataFormats.Bitmap)) { System.Windows.Interop.InteropBitmap bmp = (System.Windows.Interop.InteropBitmap)ido.GetData(DataFormats.Bitmap); if (bmp != null) { if (!Directory.Exists(savePathGetImage)) { Directory.CreateDirectory(savePathGetImage); } Util.SaveImageToFile(bmp.Clone(), savePathGetImage + pic.Name + ".jpg"); result.Add(new ImagesDetailInfo() { ImgResultPath = savePathGetImage + pic.Name + ".jpg", UnCheckWordExcelRange = pic }); } } } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } } } Dispatcher.Invoke(new System.Action(() => { System.Windows.Forms.Clipboard.Clear(); })); } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } return(result); }
/// <summary> /// 提取图片 /// </summary> private List <ImagesDetailInfo> GetImagesFromWord() { List <ImagesDetailInfo> result = new List <ImagesDetailInfo>(); try { FileOperateHelper.DeleteFolder(savePathGetImage); if (!Directory.Exists(savePathGetImage)) { Directory.CreateDirectory(savePathGetImage); } object xx = null; bool hasPic = false; int index = 1; foreach (Microsoft.Office.Interop.Word.Paragraph paragraph in Application.ActiveDocument.Paragraphs) { foreach (InlineShape ils in paragraph.Range.InlineShapes) { if (ils != null) { if (ils.Type == WdInlineShapeType.wdInlineShapePicture) { //ils.Range.Copy(); //System.Drawing.Image image = null; //Dispatcher.Invoke(new Action(() => //{ // image = System.Windows.Forms.Clipboard.GetImage(); //})); //if (image != null) //{ // image.Save(savePathGetImage + "照片-" + index + ".jpg"); // result.Add(new ImagesDetailInfo() { ImgResultPath = savePathGetImage + "照片-" + index + ".jpg", UnCheckWordRange = ils.Range }); // index++; //} //Dispatcher.Invoke(new Action(() => //{ // System.Windows.Forms.Clipboard.Clear(); //})); try { if (!hasPic) { Dispatcher.Invoke(new System.Action(() => { if (Clipboard.ContainsText()) { xx = Clipboard.GetText(); } })); } } catch (Exception ex) { } hasPic = true; try { ils.Select(); IDataObject ido = null; Dispatcher.Invoke(new Action(() => { try { Application.Selection.CopyAsPicture(); ido = Clipboard.GetDataObject(); } catch { } })); if (ido != null && ido.GetDataPresent(DataFormats.Bitmap)) { System.Windows.Interop.InteropBitmap bmp = (System.Windows.Interop.InteropBitmap)ido.GetData(DataFormats.Bitmap); if (bmp != null) { if (!Directory.Exists(savePathGetImage)) { Directory.CreateDirectory(savePathGetImage); } Util.SaveImageToFile(bmp.Clone(), savePathGetImage + "照片-" + index + ".jpg"); result.Add(new ImagesDetailInfo() { ImgResultPath = savePathGetImage + "照片-" + index + ".jpg", UnCheckWordRange = ils.Range }); index++; } } } catch { } } } } } if (hasPic) { Dispatcher.BeginInvoke(new Action(() => { System.Windows.Forms.Clipboard.Clear(); if (xx != null) { try { System.Windows.Forms.Clipboard.SetDataObject(xx); } catch (Exception ex) { } } })); } } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } return(result); }
private void InternalDeallocate(bool disposing) { if (disposing) { // free managed resources if (_bmp != null) { _bmp.Dispose(); _bmp = null; } if (_interopBmp != null) { _interopBmp = null; } } // now free all unmanaged resources if (IntPtr.Zero != _map) { UnmapViewOfFile(_map); _map = IntPtr.Zero; } if (IntPtr.Zero != _section) { CloseHandle(_section); _section = IntPtr.Zero; } var bytesToFree = _width * _height * BytesPerPixel; if (0 != bytesToFree) { GC.RemoveMemoryPressure(bytesToFree); _totalBytesAllocated -= bytesToFree; } --_activeInstances; _width = 0; _height = 0; }
private void InternalAllocate(int width, int height) { width = Math.Max(width, 1); height = Math.Max(height, 1); _width = width; _height = height; uint numBytesToAllocate = (uint)(width * height * BytesPerPixel); _section = CreateFileMapping(INVALID_HANDLE_VALUE, IntPtr.Zero, PAGE_READWRITE, 0, numBytesToAllocate, null); if (_section == IntPtr.Zero) throw new InvalidOperationException(string.Format("Unable to create file mapping for {0} x {1} pixel. ActiveInstances: {2}, NumberOfBytesAllocated={3} ", width, height, _activeInstances, _totalBytesAllocated)); _map = MapViewOfFile(_section, FILE_MAP_ALL_ACCESS, 0, 0, numBytesToAllocate); if (IntPtr.Zero == _map) { System.GC.Collect(); _map = MapViewOfFile(_section, FILE_MAP_ALL_ACCESS, 0, 0, numBytesToAllocate); } if (_map == IntPtr.Zero) { CloseHandle(_section); _section = IntPtr.Zero; string exception = string.Format("Unable to create view of file for {0} x {1} pixel. ActiveInstances: {2}, NumberOfBytesAllocated={3} ", width, height, _activeInstances, _totalBytesAllocated); width = height = 0; throw new InvalidOperationException(exception); } _bmp = new System.Drawing.Bitmap(_width, _height, _width * BytesPerPixel, System.Drawing.Imaging.PixelFormat.Format32bppArgb, _map); GC.AddMemoryPressure(numBytesToAllocate); _totalBytesAllocated += numBytesToAllocate; ++_activeInstances; _interopBmp = (System.Windows.Interop.InteropBitmap)System.Windows.Interop.Imaging.CreateBitmapSourceFromMemorySection(_section, _width, _height, System.Windows.Media.PixelFormats.Bgra32, _width * BytesPerPixel, 0); }