public void Initimage() { BitmapImage shoot = new BitmapImage(); var width = SystemInformation.VirtualScreen.Width; var height = SystemInformation.VirtualScreen.Height; Bitmap Bmp = new Bitmap(width, height); using (Graphics gfx = Graphics.FromImage(Bmp)) using (SolidBrush brush = new SolidBrush(System.Drawing.Color.FromArgb(0, 0, 0))) { gfx.FillRectangle(brush, 0, 0, width, height); } IntPtr hBitmap = Bmp.GetHbitmap(); System.Windows.Media.ImageSource WpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); shoot.BeginInit(); shoot.StreamSource = new MemoryStream(ReadImageMemory(GetScreenSnapshot())); shoot.EndInit(); disp.Source = shoot; disp_.Source = shoot; cov.Source = WpfBitmap; return; }
BitmapSource ToBitmapSource(Bitmap bmp) { BitmapSource returnSource; try { returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch { returnSource = null; } return(returnSource); }
private void BoxSlip_Loaded(object sender, RoutedEventArgs e) { //Box Information cstBoxPackage _boxInfo = Global.controller.GetBoxPackageByBoxID(Global.PrintBoxID); String BoxNumber = _boxInfo.BOXNUM; EBoxNumber = BoxNumber; //Package Information cstPackageTbl packing = Global.controller.GetPackingList(_boxInfo.PackingID, true); String ShippingNumber = packing.ShippingNum; //Shipping information cstShippingTbl shippingTbl = Global.controller.GetShippingTbl(ShippingNumber); List <cstPackageDetails> _packingDetails = Global.controller.GetPackingDetailTbl(packing.PackingId); //Sku Quantity. var SKUQty = from ls in _packingDetails where ls.BoxNumber == BoxNumber select new { SkuCount = ls.SKUQuantity }; int SkuQuantity = SKUQty.Sum(i => i.SkuCount); //User Packing Shippment String Username = Global.controller.GetSelcetedUserMaster(packing.UserID)[0].UserName.ToString(); //Grid Fill with SKU Name and Product Name var _packDetail = from ls in _packingDetails where ls.BoxNumber == BoxNumber select new { SKUNumber = ls.SKUNumber + " -" + ls.ProductName, SKUQuantity = ls.SKUQuantity }; //var _packDetail = (from ls in _packingDetails // where ls.BoxNumber == BoxNumber && // ls.SKUNumber == Global.skuNamefor // orderby ls.SKUQuantity descending //&& Max ls.SKUQuantity // select new // { // SKUNumber = ls.SKUNumber + " -" + ls.ProductName, // SKUQuantity = ls.SKUQuantity // }).Take(1).ToList(); //var pack=_packDetail.Max( Global.skuNamefor = ""; //Box Number(package Box Packing) int BoxCount = (from pd in _packingDetails group pd by pd.BoxNumber into Gpd select Gpd).Count(); //Barcode Liabrary BarcodeLib.Barcode b = new BarcodeLib.Barcode(); //Barcode for each label var sBoxNumber = b.Encode(BarcodeLib.TYPE.CODE128, BoxNumber, System.Drawing.Color.Black, System.Drawing.Color.Transparent, 400, 160); var sBoxTopNumber = b.Encode(BarcodeLib.TYPE.CODE128, BoxNumber, System.Drawing.Color.Black, System.Drawing.Color.Transparent, 380, 50); var sSOoNumber = b.Encode(BarcodeLib.TYPE.CODE128, shippingTbl.OrderID, System.Drawing.Color.Black, System.Drawing.Color.Transparent, 380, 50); var sPCKNumber = b.Encode(BarcodeLib.TYPE.CODE128, packing.PCKROWID, System.Drawing.Color.Black, System.Drawing.Color.Transparent, 380, 50); var sShippingNumber = b.Encode(BarcodeLib.TYPE.CODE128, shippingTbl.ShippingNum, System.Drawing.Color.Black, System.Drawing.Color.Transparent, 380, 50); //Image Conversion var bitmapBox = new System.Drawing.Bitmap(sBoxNumber); var bitmapBoxTop = new System.Drawing.Bitmap(sBoxTopNumber); var bitmapShipping = new System.Drawing.Bitmap(sShippingNumber); var bitmapSO = new System.Drawing.Bitmap(sSOoNumber); var bitmapPCK = new System.Drawing.Bitmap(sPCKNumber); var bBoxSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapBox.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); var bBoxTopSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapBoxTop.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); var bShippingSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapShipping.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); var bShOSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapSO.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); var bSPCKSource = Imaging.CreateBitmapSourceFromHBitmap(bitmapPCK.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); bitmapBox.Dispose(); bitmapBoxTop.Dispose(); bitmapShipping.Dispose(); bitmapSO.Dispose(); bitmapPCK.Dispose(); //assign source to images imgBoxNumber.Source = bBoxSource; lblBoxNumber.Content = BoxNumber; imgBoxNumber.Stretch = Stretch.Fill; imgBOxNumTop.Source = bBoxTopSource; lblBoxTupNumber.Content = BoxNumber; imgShipping.Source = bShippingSource; lblShipment.Content = ShippingNumber; imgSO.Source = bShOSource; lblSoNumber.Content = shippingTbl.OrderID; imgPackNum.Source = bSPCKSource; lblPckNum.Content = packing.PCKROWID; //packing Detal Info tbPackageBox.Text = BoxCount.ToString() + " [SKU QTY: " + SkuQuantity + "]"; tbCarrier.Text = shippingTbl.Carrier + " / " + shippingTbl.MDL_0; tbPoNum.Text = shippingTbl.CustomerPO.ToString(); tbDealer.Text = shippingTbl.VendorName.ToString(); tbWarehouse.Text = Global.controller.ApplicationLocation(); tbUserName.Text = Username; dgSKUinfo.ItemsSource = _packDetail.ToList(); tbPackingTime.Text = packing.EndTime.ToString("MMM dd, yyyy hh:mm tt"); lblBDate.Content = DateTime.UtcNow.ToString("dd MMM, yyyy hh:mm tt").TrimStart('0').ToString(); }
// ref. // https://stackoverflow.com/questions/26260654/wpf-converting-bitmap-to-imagesource public static BitmapSource BitmapToBitmapSource(Bitmap bitmap) { var handle = bitmap.GetHbitmap(); try { return(Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } finally { Win32Api.DeleteObject(handle); } }
/// <summary> /// Convert a bitmap to an ImageSource. /// </summary> /// <param name="bitmap">Source bitmap</param> /// <returns>ImageSource</returns> public static BitmapSource BitmapToImageSource(System.Drawing.Bitmap bitmap) { IntPtr ip = bitmap.GetHbitmap(); BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return(bitmapSource); }
protected static System.Windows.Media.ImageSource LoadIcon(string _path, int _width, int _height) { _path = Path.GetExtension(_path).ToUpper(); var _size = Math.Max(_width, _height); var _tkey = new Tuple <string, int>(_path, _size); if (_Cache.ContainsKey(_tkey)) { return(_Cache[_tkey]); } var _shfi = new SHFILEINFO(); var _flags = SHGFI_USEFILEATTRIBUTES + SHGFI_ICON; if (_size > 16) { _flags += SHGFI_LARGEICON; } else { _flags += SHGFI_SMALLICON; } var _stsz = (uint)System.Runtime.InteropServices.Marshal.SizeOf(_shfi); SHGetFileInfo(_path, FILE_ATTRIBUTE_NORMAL, ref _shfi, _stsz, _flags); var _source = Imaging.CreateBitmapSourceFromHIcon(_shfi.hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); _Cache.Add(_tkey, _source); DestroyIcon(_shfi.hIcon); return(_source); }
public static ImageSource ToImageSource(this Icon icon) { return(Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, icon.Size.Width, icon.Size.Height), BitmapSizeOptions.FromEmptyOptions())); }
private void Button_Click(object sender, RoutedEventArgs e) { string idt = (sender as Button).Name.ToString().Substring(1); string testname = ""; SqlCeCommand cmdt = new SqlCeCommand("SELECT * FROM Apps WHERE [Id] = '" + idt + "';"); cmdt.Connection = con; if (con.State == ConnectionState.Closed) { con.Open(); } SqlCeDataReader readert = cmdt.ExecuteReader(); while (readert.Read()) { testname = readert.GetString(1); } if (testname != NameExe.Content.ToString()) { if (ExpanderGrid.Height > 0) { Storyboard sbe = (Storyboard)FindResource("ExpanderClose"); sbe.Begin(this); Storyboard sbi = button.Resources["ExpIconClose"] as Storyboard; sbi.Begin(); } try { string id = (sender as Button).Name.ToString().Substring(1); SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Apps WHERE [Id] = '" + id + "';"); cmd.Connection = con; if (con.State == ConnectionState.Closed) { con.Open(); } SqlCeDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { Icon icon = null; icon = System.Drawing.Icon.ExtractAssociatedIcon(reader.GetString(2)); Bitmap bitmap = icon.ToBitmap(); IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); IconExe.Source = wpfBitmap; NameExe.Content = reader.GetString(1); } } catch (Exception ex) { if (Listbox.SelectedIndex < 0) { } else { MessageBox.Show(ex.ToString()); } } finally { con.Close(); } BackgroundChanger(); } }
public GoalKeeperStateWpfControl() { InitializeComponent(); allEllipses = new List <Ellipse> { this.nad_blin_ellipse, this.nad_trap_ellipse, this.pod_blin_ellipse, this.pod_trap_ellipse, this.v_telo_ellipse, this.v_dom_ellipse }; ResultState = GateWayThrowEnum.none; MainGrid.Background = new ImageBrush(Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources.goalkeeperstate.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); }
private static ImageSource GetIcon(string fileName) { try { if (File.Exists(fileName) == false) { return(null); } Icon icon = GetFileIcon(fileName) ?? Icon.ExtractAssociatedIcon(fileName); if (icon != null) { return(Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions())); } } catch { } return(null); }
public void list() { NameExe.Content = ""; IconExe.Source = null; Listbox.Items.Clear(); int selected = 0; SqlCeCommand cmd = new SqlCeCommand("SELECT DISTINCT Class FROM Apps ORDER BY Class ASC"); cmd.Connection = con; if (con.State == ConnectionState.Closed) { con.Open(); } SqlCeDataReader reader = cmd.ExecuteReader(); try { while (reader.Read()) { Expander expander = new Expander(); expander.Header = reader.GetString(0); expander.Style = FindResource("ExpanderBackground") as Style; expander.Foreground = (SolidColorBrush) new BrushConverter().ConvertFromString("#FFFFFFFF"); expander.FontWeight = FontWeights.Bold; expander.IsExpanded = true; Grid ItemGrid = new Grid(); Grid BackGrid = new Grid(); BackGrid.Background = (SolidColorBrush) new BrushConverter().ConvertFromString("#FF3D3D3D"); BackGrid.VerticalAlignment = VerticalAlignment.Top; BackGrid.Height = 24; ItemGrid.Children.Add(BackGrid); ItemGrid.Children.Add(expander); Listbox.Items.Add(ItemGrid); ItemsControl itemsControl = new ItemsControl(); itemsControl.Margin = new Thickness(0, 2, 0, 2); itemsControl.BorderThickness = new Thickness(0); itemsControl.Background = (SolidColorBrush) new BrushConverter().ConvertFromString("#00FFFFFF"); expander.Content = itemsControl; SqlCeCommand cmdA = new SqlCeCommand("SELECT * FROM Apps WHERE Class = '" + reader.GetString(0) + "' ORDER BY Name ASC"); cmdA.Connection = con; SqlCeDataReader readerA = cmdA.ExecuteReader(); while (readerA.Read()) { Button button = new Button(); button.Name = "A" + readerA.GetInt32(0).ToString(); button.Style = FindResource("ListButton") as Style; button.Click += Button_Click; button.MouseDoubleClick += Start_Click; button.PreviewMouseRightButtonDown += ListItem_PreviewMouseRightButtonDown; Grid grid = new Grid(); System.Windows.Controls.Image Icon = new System.Windows.Controls.Image(); Icon.HorizontalAlignment = HorizontalAlignment.Left; Icon.Height = 20; Icon.Width = 20; grid.Children.Add(Icon); TextBlock Name = new TextBlock(); Name.Foreground = (SolidColorBrush) new BrushConverter().ConvertFromString("#FFFFFFFF"); Name.FontWeight = FontWeights.DemiBold; Name.VerticalAlignment = VerticalAlignment.Center; Name.Margin = new Thickness(26, 0, 55, 0); grid.Children.Add(Name); TextBlock status = new TextBlock(); status.Foreground = (SolidColorBrush) new BrushConverter().ConvertFromString("#FFFFFFFF"); status.FontWeight = FontWeights.DemiBold; status.VerticalAlignment = VerticalAlignment.Center; status.HorizontalAlignment = HorizontalAlignment.Right; status.Margin = new Thickness(0, 0, 5, 0); grid.Children.Add(status); Icon icon = null; icon = System.Drawing.Icon.ExtractAssociatedIcon(readerA.GetString(2)); Bitmap bitmap = icon.ToBitmap(); IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); int indexStart = readerA.GetString(2).LastIndexOf('\\'); int indexStop = readerA.GetString(2).LastIndexOf('.'); string Namexe = readerA.GetString(2).Substring(indexStart + 1, indexStop - indexStart - 1); Process[] processlist = Process.GetProcesses(); foreach (Process theprocess in processlist) { if (theprocess.ProcessName == Namexe && theprocess.MainWindowHandle.ToInt64() != 0) { status.Text = "Running"; } } Name.Text = readerA.GetString(1); Icon.Source = wpfBitmap; button.Content = grid; button.Margin = new Thickness(0, 2, 0, 2); itemsControl.Items.Add(button); if (NameExe.Content.ToString() == "") { NameExe.Content = readerA.GetString(1); IconExe.Source = wpfBitmap; } if (selected == 0) { selected++; } } } } finally { con.Close(); } }
//耗时12毫秒(可能存在内存泄漏) public static ImageSource ToBitmapSource(Bitmap p_bitmap) { IntPtr hBitmap = p_bitmap.GetHbitmap(); ImageSource wpfBitmap; try { wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { //p_bitmap.Dispose(); DeleteObject(hBitmap); } return(wpfBitmap); }
public static BitmapSource ConvertToBitmapSource(System.Drawing.Bitmap gdiPlusBitmap) { IntPtr hBitmap = gdiPlusBitmap.GetHbitmap(); return(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); }
public BitmapSource CreateAndSaveImage(BackgroundWorker worker) { int width = (int)(this.width * Settings.PixelDensity); int height = (int)(this.height * Settings.PixelDensity); var bitmap = new GDI.Bitmap(width, height, GDI.Imaging.PixelFormat.Format32bppArgb); var graphics = GDI.Graphics.FromImage(bitmap); graphics.TextRenderingHint = GDI.Text.TextRenderingHint.AntiAliasGridFit; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default; int penWidth = 2; GDI.Pen pen = new GDI.Pen(GDI.Color.White, penWidth); GDI.Brush brush = new GDI.SolidBrush(GDI.Color.White); System.Drawing.StringFormat format = new System.Drawing.StringFormat(); format.Alignment = System.Drawing.StringAlignment.Center; format.LineAlignment = System.Drawing.StringAlignment.Center; if (this.border) { graphics.DrawRectangle(pen, new GDI.Rectangle(penWidth / 2, penWidth / 2, width - penWidth, height - penWidth)); } foreach (GridButton button in this.buttons) { int x = (button.X * width) / this.gridWidth; int y = (button.Y * height) / this.gridHeight; int buttonWidth = ((button.X + button.Width) * width) / this.gridWidth - x; int buttonHeight = ((button.Y + button.Height) * height) / this.gridHeight - y; if (button.ButtonType == ButtonType.Normal || button.ButtonType == ButtonType.ThreeWaySwitch || button.ButtonType == ButtonType.MultiPositionSwitch) { graphics.DrawRectangle(pen, x, y, buttonWidth, buttonHeight); } else { int minWidth = Math.Min(buttonWidth, buttonHeight); int circleX = (x + buttonWidth / 2) - minWidth / 2; int circleY = (y + buttonHeight / 2) - minWidth / 2; graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; graphics.DrawEllipse(pen, circleX, circleY, minWidth, minWidth); graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default; } GDI.Font font = new GDI.Font(GDI.SystemFonts.DefaultFont.FontFamily, (float)(button.FontSize * Settings.PixelDensity)); graphics.DrawString(button.Text, font, brush, new GDI.RectangleF(x + penWidth, y + penWidth, buttonWidth - penWidth * 2, buttonHeight - penWidth * 2), format); } graphics.Dispose(); if (worker.CancellationPending) { return(null); } if (this.outline) { OutlineBitmap(bitmap, worker, true); OutlineBitmap(bitmap, worker, false); OutlineBitmap(bitmap, worker, false); } if (worker.CancellationPending) { return(null); } bitmap.Save(Path.Combine(DirectoryInfo.FolderPath, parent.directory, this.filename), GDI.Imaging.ImageFormat.Png); var hbmp = bitmap.GetHbitmap(); var options = BitmapSizeOptions.FromEmptyOptions(); return(Imaging.CreateBitmapSourceFromHBitmap(hbmp, IntPtr.Zero, Int32Rect.Empty, options)); }
private void BtnLoad_Click(object sender, RoutedEventArgs e) { OpenFileDialog op = new OpenFileDialog(); op.Title = "Select a picture"; op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png;*.bmp|" + "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + "BMP(*.bmp)|*.bmp|" + "Portable Network Graphic (*.png)|*.png"; if (op.ShowDialog() == true) { // bitmapImage = new BitmapImage(new Uri(op.FileName)); bmp = new Bitmap(op.FileName); ImgPhoto.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } }
private static ImageSource ExtractImageSourceFromBitmap(Bitmap bitmap) { return(CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); }
public void Filter(string xfilter, string yfilter) { double[,] xFilterMatrix; double[,] yFilterMatrix; switch (xfilter) { case "Laplacian3x3": xFilterMatrix = FilterMatrix.Laplacian3x3; break; case "Laplacian5x5": xFilterMatrix = FilterMatrix.Laplacian5x5; break; case "LaplacianOfGaussian": xFilterMatrix = FilterMatrix.LaplacianOfGaussian; break; case "Gaussian3x3": xFilterMatrix = FilterMatrix.Gaussian3x3; break; case "Gaussian5x5Type1": xFilterMatrix = FilterMatrix.Gaussian5x5Type1; break; case "Gaussian5x5Type2": xFilterMatrix = FilterMatrix.Gaussian5x5Type2; break; case "Sobel3x3Horizontal": xFilterMatrix = FilterMatrix.Sobel3x3Horizontal; break; case "Sobel3x3Vertical": xFilterMatrix = FilterMatrix.Sobel3x3Vertical; break; case "Prewitt3x3Horizontal": xFilterMatrix = FilterMatrix.Prewitt3x3Horizontal; break; case "Prewitt3x3Vertical": xFilterMatrix = FilterMatrix.Prewitt3x3Vertical; break; case "Kirsch3x3Horizontal": xFilterMatrix = FilterMatrix.Kirsch3x3Horizontal; break; case "Kirsch3x3Vertical": xFilterMatrix = FilterMatrix.Kirsch3x3Vertical; break; default: xFilterMatrix = FilterMatrix.Laplacian3x3; break; } switch (yfilter) { case "Laplacian3x3": yFilterMatrix = FilterMatrix.Laplacian3x3; break; case "Laplacian5x5": yFilterMatrix = FilterMatrix.Laplacian5x5; break; case "LaplacianOfGaussian": yFilterMatrix = FilterMatrix.LaplacianOfGaussian; break; case "Gaussian3x3": yFilterMatrix = FilterMatrix.Gaussian3x3; break; case "Gaussian5x5Type1": yFilterMatrix = FilterMatrix.Gaussian5x5Type1; break; case "Gaussian5x5Type2": yFilterMatrix = FilterMatrix.Gaussian5x5Type2; break; case "Sobel3x3Horizontal": yFilterMatrix = FilterMatrix.Sobel3x3Horizontal; break; case "Sobel3x3Vertical": yFilterMatrix = FilterMatrix.Sobel3x3Vertical; break; case "Prewitt3x3Horizontal": yFilterMatrix = FilterMatrix.Prewitt3x3Horizontal; break; case "Prewitt3x3Vertical": yFilterMatrix = FilterMatrix.Prewitt3x3Vertical; break; case "Kirsch3x3Horizontal": yFilterMatrix = FilterMatrix.Kirsch3x3Horizontal; break; case "Kirsch3x3Vertical": yFilterMatrix = FilterMatrix.Kirsch3x3Vertical; break; default: yFilterMatrix = FilterMatrix.Laplacian3x3; break; } if (ImgPhoto.Source.Height > 0) //pictureBoxPreview.Image.Size.Height > 0) ( { BitmapData newbitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); byte[] pixelbuff = new byte[Math.Abs(newbitmapData.Stride) * newbitmapData.Height]; byte[] resultbuff = new byte[Math.Abs(newbitmapData.Stride) * newbitmapData.Height]; Marshal.Copy(newbitmapData.Scan0, pixelbuff, 0, pixelbuff.Length); bmp.UnlockBits(newbitmapData); double blueX = 0.0; double greenX = 0.0; double redX = 0.0; double blueY = 0.0; double greenY = 0.0; double redY = 0.0; double blueTotal = 0.0; double greenTotal = 0.0; double redTotal = 0.0; int filterOffset = 1; int calcOffset = 0; int byteOffset = 0; for (int offsetY = filterOffset; offsetY < bmp.Height - filterOffset; offsetY++) { for (int offsetX = filterOffset; offsetX < bmp.Width - filterOffset; offsetX++) { blueX = greenX = redX = 0; blueY = greenY = redY = 0; blueTotal = greenTotal = redTotal = 0.0; byteOffset = offsetY * newbitmapData.Stride + offsetX * 4; for (int filterY = -1; filterY <= filterOffset; filterY++) { for (int filterX = -1; filterX <= filterOffset; filterX++) { calcOffset = byteOffset + (filterX * 4) + (filterY * newbitmapData.Stride); blueX += (double)(pixelbuff[calcOffset]) * xFilterMatrix[filterY + filterOffset, filterX + filterOffset]; greenX += (double)(pixelbuff[calcOffset + 1]) * xFilterMatrix[filterY + filterOffset, filterX + filterOffset]; redX += (double)(pixelbuff[calcOffset + 2]) * xFilterMatrix[filterY + filterOffset, filterX + filterOffset]; blueY += (double)(pixelbuff[calcOffset]) * yFilterMatrix[filterY + filterOffset, filterX + filterOffset]; greenY += (double)(pixelbuff[calcOffset + 1]) * yFilterMatrix[filterY + filterOffset, filterX + filterOffset]; redY += (double)(pixelbuff[calcOffset + 2]) * yFilterMatrix[filterY + filterOffset, filterX + filterOffset]; } } blueTotal = 0; greenTotal = Math.Sqrt((greenX * greenX) + (greenY * greenY)); redTotal = 0; if (blueTotal > 255) { blueTotal = 255; } else if (blueTotal < 0) { blueTotal = 0; } if (greenTotal > 255) { greenTotal = 255; } else if (greenTotal < 0) { greenTotal = 0; } try { if (greenTotal < Convert.ToInt32(ThresholdSlider.Value)) { greenTotal = 0; } else { greenTotal = 255; } } catch (Exception) { throw; } if (redTotal > 255) { redTotal = 255; } else if (redTotal < 0) { redTotal = 0; } resultbuff[byteOffset] = (byte)(blueTotal); resultbuff[byteOffset + 1] = (byte)(greenTotal); resultbuff[byteOffset + 2] = (byte)(redTotal); resultbuff[byteOffset + 3] = 255; } } Bitmap resultbitmap = new Bitmap(bmp.Width, bmp.Height); BitmapData resultData = resultbitmap.LockBits(new Rectangle(0, 0, resultbitmap.Width, resultbitmap.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Marshal.Copy(resultbuff, 0, resultData.Scan0, resultbuff.Length); resultbitmap.UnlockBits(resultData); ImgResult.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(resultbitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } else { MessageBox.Show("You must load an image"); } }
private static ImageSource GetAppIcon(Assembly assembly) { var hLibrary = NativeMethods.LoadLibrary(assembly.Location); if (hLibrary != IntPtr.Zero) { var hIcon = NativeMethods.LoadIcon(hLibrary, "#32512"); if (hIcon != IntPtr.Zero) { return(Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } } return(null); }
private void Initialise(ImageSource aIcon, string aTitle, string aProduct, string aVersion) { InitializeComponent(); textBlockHeader.Text = aTitle + " has encountered a problem and needs to close. We are sorry for the inconvenience"; textBlockFooter.Text = "We have created an error report that you can send to help us improve " + aTitle + "."; image1.Source = Imaging.CreateBitmapSourceFromHBitmap(linnOssLib.Resources.Error.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); Icon = aIcon; Title = aTitle; Height = 210; iTitle = aTitle; iProduct = aProduct; iVersion = aVersion; }
public ImageSource ImageSourceFromBitmap(Bitmap bmp) { var handle = bmp.GetHbitmap(); try { return(Imaging.CreateBitmapSourceFromHBitmap(handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } finally { DeleteObject(handle); } }
protected override void OnRender(System.Windows.Media.DrawingContext dc) { if (ActualWidth <= 0 || ActualHeight <= 0 || Actors == null || Actors.Count() < 1) { return; } using (var bitmap = new Bitmap((int)ActualWidth, (int)ActualHeight)) { using (var g = Graphics.FromImage(bitmap)) { PrepareFrame(g); if (IsGridVisible) { DrawGrid(g); DrawPoints(g); } } // Copy GDI bitmap to WPF bitmap var hBitmap = bitmap.GetHbitmap(); var image = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); image.Freeze(); dc.DrawImage(image, new Rect(RenderSize)); DeleteObject(hBitmap); } }
/// <summary> /// Initializes the component. /// </summary> private void InitializeComponent() { // Synchronize initial property values to the MediaElement properties. // This is because the hosted element gets created after the MediaElement properties // might have been set. VideoView.ElementLoaded += (vs, ve) => { VideoView.UseLayoutRounding = true; VideoView.SnapsToDevicePixels = true; VideoView.Focusable = false; VideoView.IsHitTestVisible = false; VideoView.Stretch = Stretch; VideoView.StretchDirection = StretchDirection; // Wire up the layout updates VideoView.LayoutUpdated += HandleVideoViewLayoutUpdates; }; // Set some default layout properties. UseLayoutRounding = true; SnapsToDevicePixels = true; // Setup the content grid and add it as part of the user control Content = ContentGrid; // Set some layout defaults ContentGrid.HorizontalAlignment = HorizontalAlignment.Stretch; ContentGrid.VerticalAlignment = VerticalAlignment.Stretch; ContentGrid.UseLayoutRounding = true; ContentGrid.SnapsToDevicePixels = true; ContentGrid.IsHitTestVisible = false; // Setup the Subtitle View SubtitlesView.FontSize = 98; SubtitlesView.Padding = new Thickness(0); SubtitlesView.FontFamily = new FontFamily("Microsoft Sans Serif, Lucida Console, Calibri"); SubtitlesView.FontStretch = FontStretches.Condensed; SubtitlesView.FontWeight = FontWeights.Bold; SubtitlesView.TextOutlineWidth = new Thickness(4); SubtitlesView.TextForeground = Brushes.LightYellow; SubtitlesView.UseLayoutRounding = true; SubtitlesView.SnapsToDevicePixels = true; SubtitlesView.IsHitTestVisible = false; SubtitlesView.Focusable = false; SubtitlesView.HorizontalAlignment = HorizontalAlignment.Left; SubtitlesView.VerticalAlignment = VerticalAlignment.Top; // Add the subtitles control and bind the attached properties Subtitles.SetForeground(this, SubtitlesView.TextForeground); BindProperty(this, Subtitles.ForegroundProperty, SubtitlesView, nameof(SubtitlesView.TextForeground), BindingMode.TwoWay); BindProperty(this, Subtitles.OutlineBrushProperty, SubtitlesView, nameof(SubtitlesView.TextOutline), BindingMode.TwoWay); BindProperty(this, Subtitles.OutlineWidthProperty, SubtitlesView, nameof(SubtitlesView.TextOutlineWidth), BindingMode.TwoWay); BindProperty(this, Subtitles.EffectProperty, SubtitlesView, nameof(SubtitlesView.TextForegroundEffect), BindingMode.TwoWay); BindProperty(this, Subtitles.FontSizeProperty, SubtitlesView, nameof(SubtitlesView.FontSize), BindingMode.TwoWay); BindProperty(this, Subtitles.FontWeightProperty, SubtitlesView, nameof(SubtitlesView.FontWeight), BindingMode.TwoWay); BindProperty(this, Subtitles.FontFamilyProperty, SubtitlesView, nameof(SubtitlesView.FontFamily), BindingMode.TwoWay); BindProperty(this, Subtitles.TextProperty, SubtitlesView, nameof(SubtitlesView.Text), BindingMode.TwoWay); // Position the Captions View CaptionsView.HorizontalAlignment = HorizontalAlignment.Left; CaptionsView.VerticalAlignment = VerticalAlignment.Top; // Compose the control by adding overlapping children ContentGrid.Children.Add(VideoView); ContentGrid.Children.Add(SubtitlesView); ContentGrid.Children.Add(CaptionsView); // Display the control (or not) if (!Library.IsInDesignMode) { // Setup the media engine and property updates timer MediaCore = new MediaEngine(this, new MediaConnector(this)); } else { var bitmap = Properties.Resources.FFmpegMediaElementBackground; var bitmapSource = Imaging.CreateBitmapSourceFromHBitmap( bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); var controlBitmap = new WriteableBitmap(bitmapSource); VideoView.Source = controlBitmap; } // Bind Content View Properties BindProperty(VideoView, HorizontalAlignmentProperty, this, nameof(HorizontalContentAlignment), BindingMode.OneWay); BindProperty(VideoView, VerticalAlignmentProperty, this, nameof(VerticalContentAlignment), BindingMode.OneWay); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var img = (Icon)value; if (img != null) { var bitmap = img.ToBitmap(); bitmap.MakeTransparent(); var hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return(wpfBitmap); } return(null); }
private void ImageInit(object sender, EventArgs e) { Bitmap bitmap = Properties.Resources.explorer; CentralImg.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); // Image is an image source }
void updateIcons(string uri) { Bitmap bmp = new Bitmap(Application.GetResourceStream(new Uri(uri, UriKind.Relative)).Stream); this.trayIcon.Icon = System.Drawing.Icon.FromHandle(bmp.GetHicon()); this.imgIcon.Source = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); this.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri(uri, UriKind.Relative)).Stream); }
/// <summary> /// This method will capture the current Cursor by using User32 Code /// </summary> /// <returns>A IElement with the Mouse Cursor as Image in it.</returns> public static bool TryGetCurrentCursor(out BitmapSource bitmapSource, out NativePoint location) { bitmapSource = null; location = NativePoint.Empty; var cursorInfo = CursorInfo.Create(); if (!NativeCursorMethods.GetCursorInfo(ref cursorInfo)) { return(false); } if (cursorInfo.Flags != CursorInfoFlags.Showing) { return(false); } using (var safeIcon = NativeIconMethods.CopyIcon(cursorInfo.CursorHandle)) { if (!NativeIconMethods.GetIconInfo(safeIcon, out var iconInfo)) { return(false); } using (iconInfo.BitmaskBitmapHandle) using (iconInfo.ColorBitmapHandle) { var cursorLocation = User32Api.GetCursorLocation(); bitmapSource = Imaging.CreateBitmapSourceFromHIcon(safeIcon.DangerousGetHandle(), Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); location = new NativePoint(cursorLocation.X - iconInfo.Hotspot.X, cursorLocation.Y - iconInfo.Hotspot.Y); } } return(true); }
private static BitmapSource LoadBitmap(Bitmap source) { IntPtr ip = source.GetHbitmap(); BitmapSource bs = null; try { bs = Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(ip); } return(bs); }
public static BitmapSource CaptureScreen(Rect rect) { using (var screenBmp = new Bitmap((int)rect.Width, (int)rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { using (var bmpGraphics = Graphics.FromImage(screenBmp)) { bmpGraphics.CopyFromScreen((int)rect.X, (int)rect.Y, 0, 0, screenBmp.Size); return(Imaging.CreateBitmapSourceFromHBitmap(screenBmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); } } }
public static BitmapSource GetImageSource(System.Drawing.Bitmap bitmap) { return(Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())); }
public static BitmapSource ToBitmapSource(this System.Drawing.Bitmap bmp) { BitmapSource returnSource; try { returnSource = Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } catch { returnSource = null; System.Environment.Exit(1); } return(returnSource); }