1

I am creating a WPF application. I used a barcode library to generate barcode of each employee ID. But when I try to assign barcode image to Image Control then it shows following error:
cannot implicitly convert type system.drawing.image to system.windows.controls.image
Here is my code:

public partial class Card : Window
{
    private PrintDialog dialog;
    Image myimg;

    public Card(String a, String b, String c, String d, String e, String f, String g, String h, String i)
    {
        InitializeComponent();
        myimg = Code128Rendering.MakeBarcodeImage(h, 2, true);
        image2 = myimg;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        dialog = new PrintDialog();
        dialog.PrintVisual(canvas1, "Employee Card");
    }
}

Please help me out.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Azeem
  • 2,904
  • 18
  • 54
  • 89
  • [This answer](http://stackoverflow.com/a/8491098/1136211) might help you converting from System.Drawing.Image to [BitmapSource](http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.aspx), which would be assigned to myimg.[Source](http://msdn.microsoft.com/en-us/library/system.windows.media.imagesource.aspx). – Clemens Apr 28 '12 at 17:36

1 Answers1

1

The error should be pretty obvious, the field myImg or image2 are of type system.windows.controls.image, while the method creates a system.drawing.image.

Without knowing the purpose of the field it's hard to say what you should do, but if you just want a reference you need to qualify the type or change your using statements to not include the controls namespace.

e.g. for qualified reference:

System.Drawing.Image myImg;

If image2 caused the error you probably want to convert the drawing.image to an ImageSource of some kind (it's the base WPF image type) and assign it to the image2.Source instead. I am sure that there is a question about this convertion on SO somewhere if you do not know how to do that.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Is there some way I could assign myimg = Code128Rendering.MakeBarcodeImage(h, 2, true); without any errors? This is my ultimate purpose. – Azeem Apr 28 '12 at 14:37
  • @NidaSulheri: What would that assignment achieve? Did that line cause the error? Did you qualify the field as shown? – H.B. Apr 28 '12 at 14:38
  • I am creating Employee Card Creation Application in which I used a thirdparty barcode generation library GenCode128. How can I convert Code128Rendering.MakeBarcodeImage(h, 2, true) to ImageSource? Your help will be highly appreciated. And yes It shows error on conversion [myimg = Code128Rendering.MakeBarcodeImage(h, 2, true);] – Azeem Apr 28 '12 at 14:51
  • @NidaSulheri: As i said in my answer, **look around on SO**. – H.B. Apr 28 '12 at 14:57
  • Can you provide me some links related to this conversion? I could not able to find this topic on SO. – Azeem Apr 28 '12 at 15:01
  • @NidaSulheri: ...or create a [Bitmap from the Image](http://msdn.microsoft.com/en-us/library/ts25csc8.aspx) and use any of the other bitmap-specific methods, like [interop](http://stackoverflow.com/a/96470/546730). – H.B. Apr 28 '12 at 15:27