Esai
Esai

Reputation: 1

Can't resolve System.Windows.Control.Image from being created instead of System.Drawing.Image

I'm working on moving a C# program into a WPF application and in order to export files from records I need to use the following code:

// Construct _imgfiles from the records
string _imgfile = _maindest + "Images\\" + _pageseq.ToString("0000") + ".tif";
if ((_recpage.FileType == FileType.ImageFile) && (_recpage.ImageType == ImageFileType.TIFF))
{
    // Copy the record into it's designated directory
    File.Copy(_recpage.FileLocation, _imgfile);
    totalRecords++;
}
else if ((_recpage.FileType == FileType.ImageFile) && (_recpage.ImageType == ImageFileType.JPEG || _recpage.ImageType == ImageFileType.BMP))
{
    Image _tmpimg = Image.FromFile(_recpage.FileLocation);
    _tmpimg.Save(_imgfile, _conn.EncoderInfo, _conn.ImgEncParams);
    _totalpages++;
}

I run into an error at the following line:

Image _tmpimg = Image.FromFile(_recpage.FileLocation);

Where the 'Image' being created is not one of System.Drawing.Image that I need in order to access the 'FromFile' function but one from System.Windows.Control.Image. Which is still needed in other areas of the code, so taking it out would not be an appropriate fix.

I tried calling it directly:

System.Drawing.Image _tmpimg = System.Drawing.Image.FromFile(_recpage.FileLocation);

With no luck, as well as Aliasing the namespace which didn't do any good either:

using DrawingImage = System.Drawing.Image;

DrawingImage _tmpimg = DrawingImage.FromFile(_recpage.FileLocation);

I had read in another question that System.Drawing.Image shouldn't be used in WPF apps however that question was related to bitmaps and was over 7 years ago, so I don't know how relevant it is here. Any help?

Upvotes: -2

Views: 109

Answers (1)

Hui Liu
Hui Liu

Reputation: 1078

jmcilhinney is right.

To use System.Drawing.Image in WPF and successfully run System.Drawing.Image temp = System.Drawing.Image.FromFile("Image address"); you need to set the following.

In the WPF APP(.NET Framework) you need to reference System.Drawing:

References->Add Reference->Assemblies->Framework-> Find System.Drawing and add

In In WPF Application You need to install System.Drawing.Common via NuGet

Upvotes: 0

Related Questions