Benutzer mit den meisten Antworten
bitmap von Image.Source holen - geht das? WPF

Frage
-
Hallo,
ich versuche über die Kamera in WPF einen QR Code einzuscannen. Die Methode, die den QR Code einscannt braucht eine Bitmap. Ich habe einen Image Control (genannt: frameHolder) Nun weiß ich nicht wie ich die Bitmap von frameHolder herbekommen soll. Hat jemand eine Idee, ich bin hier am verzweifeln.
Danke im VorrausAdrian
Antworten
-
Hi Adrian,
das geht auch in WPF recht einfach, z.B. so:XAML:
<Window x:Class="WpfApp1CS.Window43" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1CS" mc:Ignorable="d" Title="Window43" Height="300" Width="300"> <Window.Resources> <local:Window43VM x:Key="vm"/> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <Image Source="{Binding Bild}"/> </Grid> </Window>
Dazu der ViewModel:public class Window43VM { public BitmapImage Bild { get { return GetBitmapImage(); } } private BitmapImage GetBitmapImage() { var bmp = GetBitMap(); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); BitmapImage img = new BitmapImage(); img.BeginInit(); ms.Seek(0, SeekOrigin.Begin); img.StreamSource = ms; img.EndInit(); return img; } private System.Drawing.Bitmap GetBitMap() { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(200, 50); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp); g.DrawString("Test-Zeichenkette", new System.Drawing.Font("Arial", 8), System.Drawing.Brushes.Red, new System.Drawing.PointF(10, 10)); return bmp; } }
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Als Antwort markiert michad2151 Montag, 9. April 2018 13:40
Alle Antworten
-
Hi Adrian,
Du kannst Doch eine Bitmap (BitmapImage) selbst erzeugen (instanziieren) und dann an das Image Control binden.--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks -
Hey,
also mein code sieht wiefolgt aus: 1. kamera an imgcontrol anknüpfenvoid Cam_NewFrame(object sender, NewFrameEventArgs eventArgs) { try { System.Drawing.Bitmap img = (Bitmap)eventArgs.Frame.Clone(); MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Bmp); ms.Seek(0, SeekOrigin.Begin); BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); bi.Freeze(); Dispatcher.BeginInvoke(new ThreadStart(delegate { frameHolder.Source = bi; })); } catch (Exception ex) { } }
und hier die Methode die den QR Code decoded (braucht eine Bitmap:)
Result result = Reader.Decode(#hier ist das problem :D#);
brauch ich nicht eine uri, um das Image Control an das BitmapImage zu binden? Ich bin überfragt.
Grüße
- Bearbeitet michad2151 Montag, 9. April 2018 09:49
-
Hi Adrian,
eine Uri brauchst Du, wenn Du ein Bild direkt in eine ImageSource laden willst.Alternativ kannst Du auch eine RenderTargetBitmap binden, die Du per Code mit den Pixels aus einer Bitmap füllst. Ich hatte hier mal ein ähnliches Beispiel gepostet, was vielleicht als Anregung dienen kann.
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks -
Hi Adrian,
das geht auch in WPF recht einfach, z.B. so:XAML:
<Window x:Class="WpfApp1CS.Window43" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1CS" mc:Ignorable="d" Title="Window43" Height="300" Width="300"> <Window.Resources> <local:Window43VM x:Key="vm"/> </Window.Resources> <Grid DataContext="{StaticResource vm}"> <Image Source="{Binding Bild}"/> </Grid> </Window>
Dazu der ViewModel:public class Window43VM { public BitmapImage Bild { get { return GetBitmapImage(); } } private BitmapImage GetBitmapImage() { var bmp = GetBitMap(); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); BitmapImage img = new BitmapImage(); img.BeginInit(); ms.Seek(0, SeekOrigin.Begin); img.StreamSource = ms; img.EndInit(); return img; } private System.Drawing.Bitmap GetBitMap() { System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(200, 50); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp); g.DrawString("Test-Zeichenkette", new System.Drawing.Font("Arial", 8), System.Drawing.Brushes.Red, new System.Drawing.PointF(10, 10)); return bmp; } }
--
Viele Grüsse
Peter Fleischer (ehem. MVP)
Meine Homepage mit Tipps und Tricks- Als Antwort markiert michad2151 Montag, 9. April 2018 13:40