public static BitmapSource LoadBitmap(System.Drawing.Bitmap source) { handle = source.GetHbitmap(); bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); DeleteObject(handle); return bitmapSource; }
public static BitmapSource LoadBitmap(System.Drawing.Bitmap source) { var ip = source.GetHbitmap(); var bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); DeleteObject(ip); return bs; }
public static BitmapImage Bitmap2BitmapImage(System.Drawing.Bitmap bitmap) { IntPtr hBitmap = bitmap.GetHbitmap(); BitmapSource bmpSource; BitmapImage bitmapImage = new BitmapImage(); try { bmpSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); BitmapEncoder enc = new BmpBitmapEncoder(); MemoryStream memoryStream = new MemoryStream(); enc.Frames.Add(BitmapFrame.Create(bmpSource)); enc.Save(memoryStream); memoryStream.Position = 0; bitmapImage.BeginInit(); bitmapImage.StreamSource = memoryStream; bitmapImage.EndInit(); } finally { DeleteObject(hBitmap); } return bitmapImage; }
public static BitmapSource loadBitmap(System.Drawing.Bitmap source) { return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging .BitmapSizeOptions .FromEmptyOptions()); }
/// <summary> /// Convert GDI bitmap into a WPF BitmapSource /// </summary> /// <param name="source"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static BitmapSource ConvertGDI_To_WPF(System.Drawing.Bitmap bmp) { BitmapSource bms = null; if (bmp != null) { IntPtr h_bm = bmp.GetHbitmap(); bms = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(h_bm, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } return bms; }
public static ImageSource ToImageSource(System.Drawing.Bitmap bitmap) { var hbitmap = bitmap.GetHbitmap(); try { var imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromWidthAndHeight(bitmap.Width, bitmap.Height)); return imageSource; } finally { DeleteObject(hbitmap); } }
public static BitmapSource BitmapToBitmapSource(System.Drawing.Bitmap bitmap) { intPointer = bitmap.GetHbitmap(); bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(intPointer, IntPtr.Zero, System.Windows.Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(intPointer); return bitmapSource; }
private static BitmapSource ToBitmapSource(System.Drawing.Bitmap source) { BitmapSource bitSrc = null; var hBitmap = source.GetHbitmap(); bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return bitSrc; }
public static ImageSource convertBitmapToBitmapSource(System.Drawing.Bitmap bitmap) { using (bitmap) { System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); return bitmapSource; } }
private void SetImageSource(System.Drawing.Bitmap bmp) { if (bmp == null) return; // how to convert a bitmap to an imagesource http://blog.laranjee.com/how-to-convert-winforms-bitmap-to-wpf-imagesource/ // TODO - watch out for memory leaks using system.drawing.bitmaps in managed code, see here http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/4e213af5-d546-4cc1-a8f0-462720e5fcde // need to call Dispose manually somewhere, or perhaps use a WPF native structure instead of bitmap? var hbitmap = bmp.GetHbitmap(); var imageSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); image.Source = imageSource; }
/// <summary> /// 从Bitmap创建可供WPF Image控件使用的BitmapSource对象 /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public static BitmapSource BitmapToImageSource(System.Drawing.Bitmap bitmap) { BitmapSource destination; IntPtr hBitmap = bitmap.GetHbitmap(); BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions(); destination = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, System.Windows.Int32Rect.Empty, sizeOptions); destination.Freeze(); return destination; }
void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image) { IntPtr hBitMap = image.GetHbitmap(); BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); bmaps.Freeze(); Dispatcher.Invoke((Action)(() => { if (Center.X > 0) { try { prova.Content = String.Format(" Codigo: {0}", Center.X); item.Content = String.Format(" Nº Itens: {0}", Center.Y); TextReader respostas = new StreamReader(@"" + Center.X + ".txt"); string R_1 = respostas.ReadLine(); string R_2 = respostas.ReadLine(); string R_3 = respostas.ReadLine(); string R_4 = respostas.ReadLine(); string R_5 = respostas.ReadLine(); string R_6 = respostas.ReadLine(); string R_7 = respostas.ReadLine(); string R_8 = respostas.ReadLine(); string R_9 = respostas.ReadLine(); string R_0 = respostas.ReadLine(); item1.Content = String.Format(" Item 01: {0}", R_1); item2.Content = String.Format(" Item 02: {0}", R_2); item3.Content = String.Format(" Item 03: {0}", R_3); item4.Content = String.Format(" Item 04: {0}", R_4); item5.Content = String.Format(" Item 05: {0}", R_5); item6.Content = String.Format(" Item 06: {0}", R_6); item7.Content = String.Format(" Item 07: {0}", R_7); item8.Content = String.Format(" Item 08: {0}", R_8); item9.Content = String.Format(" Item 09: {0}", R_9); item10.Content = String.Format(" Item 10: {0}", R_0); string resps = R_1 + ";" + R_2 + ";" + R_3 + ";" + R_4 + ";" + R_5 + ";" + R_6 + ";" + R_7 + ";" + R_8 + ";" + R_9 + ";" + R_0; refer.Content = String.Format("todas: {0}", resps); conexao((int)Center.X, resps); respostas.Close(); } catch { } } pictureBoxMain.Source = bmaps; }), DispatcherPriority.Render, null); DeleteObject(hBitMap); }
public static BitmapSource loadBitmap(System.Drawing.Bitmap source) { IntPtr ip = source.GetHbitmap(); BitmapSource bs = null; try { bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(ip); } return bs; }
public static BitmapSource ToWPFBitmap(System.Drawing.Bitmap bitmap) { var hBitmap = bitmap.GetHbitmap(); BitmapSource source; try { source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(hBitmap); } return source; }
public TileSet(System.Drawing.Bitmap source, int tileWidth, int tileHeight) { _source = new WriteableBitmap(Imaging.CreateBitmapSourceFromHBitmap( source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(source.Width, source.Height))); TileWidth = tileWidth; TileHeight = tileHeight; _tilesPerRow = (int)_source.Width / TileWidth; _bytesPerPixel = _source.Format.BitsPerPixel / BITS_PER_BYTE; _stride = _source.PixelWidth * _bytesPerPixel; _originalPixels = new byte[_stride * _source.PixelHeight]; _source.CopyPixels(_originalPixels, _stride, 0); _tiles = new List<CroppedBitmap>(); GenerateTiles(); }
public static BitmapSource BitmapToBitmapSource(System.Drawing.Bitmap bitmap) { if (bitmap == null) throw new ArgumentNullException("bitmap"); lock (bitmap) { IntPtr hBitmap = bitmap.GetHbitmap(); try { return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(hBitmap); } } }
public static ImageSource ConvertToImageSource(System.Drawing.Bitmap bmp) { if (bmp == null) return null; ImageSource imageSource; var hbitmap = bmp.GetHbitmap(); try { imageSource = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); } finally { DeleteObject(hbitmap); } return imageSource; }
private static BitmapSource ToBitmapSource(System.Drawing.Bitmap src) { BitmapSource BitSrc = null; var hbmp = src.GetHbitmap(); try { BitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hbmp, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch (Win32Exception) { BitSrc = null; } finally { NativeMethods.DeleteObject(hbmp); } return BitSrc; }
private static BitmapSource toBitmapSource(System.Drawing.Bitmap source) { var hBitmap = source.GetHbitmap(); try { var ret = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return ret; } catch (Win32Exception) { return null; } finally { DeleteObject(hBitmap); } }
/// <summary> /// Bitmap文件转化为Image文件 /// </summary> /// <param name="bitmap"></param> /// <returns></returns> public static ImageSource GetImageSourceFromBitmap(System.Drawing.Bitmap bitmap) { IntPtr ip = bitmap.GetHbitmap(); BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); DeleteObject(ip); return bitmapSource; }
public BitmapSource CreateBitmapSourceFromBitmap(System.Drawing.Bitmap bitmap) { return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); }
public BitmapSource ToBitmapSource(System.Drawing.Bitmap source) { BitmapSource bitSrc = null; var hBitmap = source.GetHbitmap(); try { bitSrc = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch (Win32Exception) { bitSrc = null; } return bitSrc; }
public PICTDESCbmp(System.Drawing.Bitmap bitmap) { hbitmap = bitmap.GetHbitmap(); // gpr: What about palettes? }
void processor_NewTargetPosition(System.Drawing.Bitmap image) { IntPtr hBitMap = image.GetHbitmap(); BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); bmaps.Freeze(); Dispatcher.Invoke((Action)(() => { pictureBoxMain.Source = bmaps; }), DispatcherPriority.Render, null); }
//Bitmap-to-ImageSource converter public static ImageSource BitmapToImageSource(System.Drawing.Bitmap bmp) { IntPtr hBitmap = bmp.GetHbitmap(); ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); bmp.Dispose(); return wpfBitmap; }
/// <summary> /// This is a helper function so you can assign the Icon directly /// from a Bitmap, such as one from a resources file. /// </summary> /// <param name="value"></param> protected void SetIconFromBitmap(System.Drawing.Bitmap value) { IntPtr hBitmap = value.GetHbitmap(); try { BitmapSource b = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); Icon = b; } finally { DeleteObject(hBitmap); } }
/// <summary> /// FreeImage method /// </summary> /// <param name="bmp"></param> /* public static void FI_ConvertSave(Bitmap bmp) { if (bmp != null) { using (FreeImageAPI.FreeImageBitmap fiBitmap = FreeImageAPI.FreeImageBitmap.FromHbitmap(bmp.GetHbitmap())) { if (fiBitmap.ColorDepth > 24) { fiBitmap.ConvertColorDepth(FreeImageAPI.FREE_IMAGE_COLOR_DEPTH.FICD_08_BPP); } for (int i = 16; i < 256; i++) fiBitmap.Palette.Data[i] = new FreeImageAPI.RGBQUAD(Color.White); FreeImageAPI.Palette pl = new FreeImageAPI.Palette(256); pl.CreateGrayscalePalette(); //quantize using the NeuQuant neural-net quantization algorithm fiBitmap.Quantize(FreeImageAPI.FREE_IMAGE_QUANTIZE.FIQ_NNQUANT, 16, pl); fiBitmap.Save("test_FreeImageOutput.png", FreeImageAPI.FREE_IMAGE_FORMAT.FIF_PNG, FreeImageAPI.FREE_IMAGE_SAVE_FLAGS.PNG_Z_BEST_COMPRESSION); //bmp = fiBitmap.ToBitmap(); //ms = new MemoryStream(); //fiBitmap.Save(ms, FreeImageAPI.FREE_IMAGE_FORMAT.FIF_PNG, FreeImageAPI.FREE_IMAGE_SAVE_FLAGS.PNG_Z_DEFAULT_COMPRESSION); } bmp.Dispose(); } } */ // From wischik.com static Bitmap CopyToBpp(System.Drawing.Bitmap b, int bpp) { if (bpp != 1 && bpp != 8) throw new System.ArgumentException("1 or 8", "bpp"); // Plan: built into Windows GDI is the ability to convert // bitmaps from one format to another. Most of the time, this // job is actually done by the graphics hardware accelerator card // and so is extremely fast. The rest of the time, the job is done by // very fast native code. // We will call into this GDI functionality from C#. Our plan: // (1) Convert our Bitmap into a GDI hbitmap (ie. copy unmanaged->managed) // (2) Create a GDI monochrome hbitmap // (3) Use GDI "BitBlt" function to copy from hbitmap into monochrome (as above) // (4) Convert the monochrone hbitmap into a Bitmap (ie. copy unmanaged->managed) int w = b.Width; int h = b.Height; IntPtr hbm = b.GetHbitmap(); // this is step (1) // // Step (2): create the monochrome bitmap. // "BITMAPINFO" is an interop-struct which we define below. // In GDI terms, it's a BITMAPHEADERINFO followed by an array of two RGBQUADs GDI32.BITMAPINFOHEADER bmi = new GDI32.BITMAPINFOHEADER(); bmi.biSize = 40; // the size of the BITMAPHEADERINFO struct bmi.biWidth = w; bmi.biHeight = h; bmi.biPlanes = 1; // "planes" are confusing. We always use just 1. Read MSDN for more info. bmi.biBitCount = (ushort)bpp; // ie. 1bpp or 8bpp bmi.biCompression = (uint)GdiFlags.BI_RGB; // ie. the pixels in our RGBQUAD table are stored as RGBs, not palette indexes bmi.biSizeImage = (uint)(((w + 7) & 0xFFFFFFF8) * h / 8); bmi.biXPelsPerMeter = 1000000; // not really important bmi.biYPelsPerMeter = 1000000; // not really important // Now for the colour table. uint ncols = (uint)1 << bpp; // 2 colours for 1bpp; 256 colours for 8bpp bmi.biClrUsed = ncols; bmi.biClrImportant = ncols; bmi.cols = new uint[256]; // The structure always has fixed size 256, even if we end up using fewer colours if (bpp == 1) { bmi.cols[0] = GDI32.MAKERGB(0, 0, 0); bmi.cols[1] = GDI32.MAKERGB(255, 255, 255); } else { for (int i = 0; i < ncols; i++) bmi.cols[i] = GDI32.MAKERGB(i, i, i); } // For 8bpp we've created an palette with just greyscale colours. // You can set up any palette you want here. Here are some possibilities: // greyscale: for (int i=0; i<256; i++) bmi.cols[i]=MAKERGB(i,i,i); bmi.biClrUsed = 216; bmi.biClrImportant = 216; int[] colv = new int[6] { 0, 51, 102, 153, 204, 255 }; for (int i = 0; i < 216; i++) bmi.cols[i] = GDI32.MAKERGB(colv[i / 36], colv[(i / 6) % 6], colv[i % 6]); // rainbow: bmi.biClrUsed=216; bmi.biClrImportant=216; int[] colv=new int[6]{0,51,102,153,204,255}; // for (int i=0; i<216; i++) bmi.cols[i]=GDI32.MAKERGB(colv[i/36],colv[(i/6)%6],colv[i%6]); // optimal: a difficult topic: http://en.wikipedia.org/wiki/Color_quantization // // Now create the indexed bitmap "hbm0" IntPtr bits0; // not used for our purposes. It returns a pointer to the raw bits that make up the bitmap. IntPtr hbm0 = GDI32.CreateDIBSection(IntPtr.Zero, ref bmi, (uint)GdiFlags.DIB_RGB_COLORS, out bits0, IntPtr.Zero, 0); // // Step (3): use GDI's BitBlt function to copy from original hbitmap into monocrhome bitmap // GDI programming is kind of confusing... nb. The GDI equivalent of "Graphics" is called a "DC". IntPtr sdc = USER32.GetDC(IntPtr.Zero); // First we obtain the DC for the screen // Next, create a DC for the original hbitmap IntPtr hdc = GDI32.CreateCompatibleDC(sdc); GDI32.SelectObject(hdc, hbm); // and create a DC for the monochrome hbitmap IntPtr hdc0 = GDI32.CreateCompatibleDC(sdc); GDI32.SelectObject(hdc0, hbm0); // Now we can do the BitBlt: GDI32.BitBlt(hdc0, 0, 0, w, h, hdc, 0, 0, TernaryRasterOperations.SRCCOPY); // Step (4): convert this monochrome hbitmap back into a Bitmap: System.Drawing.Bitmap b0 = System.Drawing.Bitmap.FromHbitmap(hbm0); // // Finally some cleanup. GDI32.DeleteDC(hdc); GDI32.DeleteDC(hdc0); USER32.ReleaseDC(IntPtr.Zero, sdc); GDI32.DeleteObject(hbm); GDI32.DeleteObject(hbm0); // return b0; }
public ImageLBI(System.Drawing.Bitmap bitmap) : base(CapturedItemsListController.Instance) { System.Windows.Media.Imaging.BitmapSource image = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); this.image = image; Tag = new ImageHash(bitmap); this.MinHeight = 25; double width = image.Width; double height = image.Height; int scale_to = 200; Scale(ref width, ref height, scale_to); this.ToolTip = new Image { Source = image, Width = width, Height = height }; this.Title = "IMAGE #" + counter++ + " (" + controller.ActiveProcess.ProcessName + ")"; }
internal Bitmap(System.Drawing.Bitmap bitmap) { this.hbitmap = bitmap.GetHbitmap(); }
void UpdatePPMask(System.Drawing.Bitmap bitmap) { IntPtr screenDc = Win32.GetDC(IntPtr.Zero); IntPtr memDc = Win32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(System.Drawing.Color.FromArgb(0)); oldBitmap = Win32.SelectObject(memDc, hBitmap); Win32.Size size = new Win32.Size(bitmap.Width, bitmap.Height); Win32.Point pointSource = new Win32.Point(0, 0); Win32.Point topPos = new Win32.Point(Left, Top); Win32.BLENDFUNCTION blend = new Win32.BLENDFUNCTION(); blend.BlendOp = Win32.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Win32.SelectObject(memDc, oldBitmap); Win32.DeleteObject(hBitmap); } Win32.DeleteDC(memDc); } }