0

-I am trying to write a method for a Card Scanner and I am quite new in C# :)

-The application I wrote can scan and gets the images from the Scanner, but I can not show them on the WPF.

-The SDK is written for Windows Forms, so I have to do a conversion from Bitmap to BitmapImage (What I actually did).

-When I try to add a Source with XAML, it works, but it should get the Source from the Memory! But I have no Idea how it supposed to be on C#

Please help! I´ve tried almost everything. Thank you

class StartScan
{
    protected internal SDKWrapper sdk = new SDKWrapper();
    MainWindow mw;

    public void Scan()
    {
        try
        {
            ShowImage(sdk.ScanAndGetImage(), false);
        }
        catch (Exception e)
        {           
            System.Console.WriteLine(e.ToString());
        }
    }

    private void ShowImage(System.Drawing.Image image, bool converted)
    {
        mw = new MainWindow();

        if (image == null)
        {
            System.Console.WriteLine("IMAGE=NULL");
            return;
        }
        int displayW =(int) mw.CardBoxRect.Width;
        int displayH = (int) mw.CardBoxRect.Height;

        Bitmap b = new Bitmap(displayW,displayH);

        mw.CardBox.Source = BitmapToImageSource(b);
    }

    BitmapImage BitmapToImageSource(Bitmap bitmap)
    {
        using (MemoryStream memory = new MemoryStream())
        {
            System.Console.WriteLine(bitmap.ToString());
            bitmap.Save(memory, ImageFormat.Bmp);
            memory.Position = 0;
            BitmapImage bitmapimage = new BitmapImage();

            bitmapimage.BeginInit();
            bitmapimage.StreamSource = memory;

            bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapimage.EndInit();
            bitmapimage.Freeze();
            return bitmapimage;
        }
    }
}
Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
A. Cinar
  • 1
  • 1
  • Have you Tried BitmapFrame, which you can obtain from reading a memory stream with a BitmapDecoder ? – MikeT Mar 31 '17 at 09:17
  • 1
    Your scanned image is stored in `image` that is passed in to `ShowImage`. You then create a new Bitmap object and convert it to a BitmapImage, both of these will be empty since you didn't reference the image when creating them. You should create a Bitmap from the Image. [Look here](http://stackoverflow.com/questions/10077498/show-drawing-image-in-wpf) for answers on how to do this. – Lithium Mar 31 '17 at 09:19
  • @MikeT FYI You can also create a BitmapImage from a stream, by setting its `StreamSource` property. – Clemens Mar 31 '17 at 09:22
  • Besides what Lithium has said, you are also creating a new MainWindow in ShowImage, which makes no sense. – Clemens Mar 31 '17 at 09:24
  • @Clemens i know you can, but you can get odd behaviour when the stream is disposed before the image is loaded, when using the decoder and frame directly i find you get much more predicable results – MikeT Mar 31 '17 at 09:26
  • For what Lithium has pointed out, you may try to simply cast `image` to `Bitmap`, like `var b = (Bitmap)image;` – Clemens Mar 31 '17 at 09:26
  • @MikeT Both BitmapFrame and BitmapImage behave identically with respect to the `BitmapCacheOption.OnLoad` option, which ensures that the BitmapSource is loaded before its source stream is closed. – Clemens Mar 31 '17 at 09:27
  • @Lithium Thank you for your comment. i already red that post and tried what you wrote, but there were no changes. – A. Cinar Mar 31 '17 at 10:03
  • @A.Cinar I'm pretty sure your problem then relies in what Clemens said. You are creating a new MainWindow in the ShowImage method, but that window is never shown (unless you are calling show in the constructor). Have you tried debugging at all? Set a breakpoint and check that each object is loading correctly (Image, Bitmap, BitmapImage). – Lithium Mar 31 '17 at 10:27
  • You were right, the Problem was in the MainWindow.Thank you soo much! – A. Cinar Apr 04 '17 at 09:11
  • But I have another issue now, it shows the scanned image, but I am getting just a black image, nothing else, do you have any suggestions? – A. Cinar Apr 04 '17 at 09:12

0 Answers0