-3

I did some prototyping in WinForm app and the code works for me:

using System.Drawing;
using System.Drawing.Imaging;

.............

Image img0 = null;
img0 = Image.FromFile(filename);
PropertyItem[] propItems = img0.PropertyItems;

However when I copied it to a WPF application I'm getting errors & incompatibilities between references.

I tried to Add .NET references System.Drawing and System.Drawing.Imaging, but there is a conflict with System.Windows.Control.Image. Could anybody advise how to rewrite the above code to get it working in WPF.

Joe
  • 118
  • 2
  • 6
  • I _tend_ to say "do it from scratch" rather than porting the Winform Code to WPF. Except you manage (and want) to [host the respective winform control in WPF](https://learn.microsoft.com/en-US/dotnet/framework/wpf/advanced/walkthrough-hosting-a-windows-forms-control-in-wpf). – Fildor Feb 28 '18 at 13:01
  • 1
    If it's a prototype I agree with Fildor, rewrite it from scratch in WPF. If you copy and paste it into your actual app it was never prototyping, yet will come with all the problems that prototype code generally has. – Callum Bradbury Feb 28 '18 at 13:16
  • No problem in rewriting these three lines, but I can't find the equivalents, where I load the file by giving filename and get instant access to the PropertyItems. Could you please advice the equivalents for Image.FromFile and PropertyItem in WPF. – Joe Feb 28 '18 at 13:20

1 Answers1

2

Whenever you find yourself dealing with class names conflicts, you can set an using to specify which namespace you are talking about when using that class:

using System.Drawing;
using System.Drawing.Imaging;
using Image = System.Drawing.Image;

OR, you can explicitly define the namespace on your declaration:

System.Drawing.Image img0 = null;
img0 = System.Drawing.Image.FromFile(filename);

I personally prefer the first approach if I know I won't use both Image (System.Drawing's and System.Windows.Controls's) classes

Now, if you are trying to programatically create a System.Windows.Controls.Image, check: Setting WPF image source in code

Mari Faleiros
  • 898
  • 9
  • 24
  • The other option is to replace Image.FromFile with equivalent, but Image in System.Windows.Control is not the same as from System.Drawing, so it does't have FromFile method. WinForm code was an easy way of getting the PropertyItems values. – Joe Feb 28 '18 at 13:18
  • What exactly are you trying to accomplish? Are you trying to create a dynamic Image control? If so, you need to set System.Windows.Controls.Image's ImageSource property with a new BitmapImage object – Mari Faleiros Feb 28 '18 at 13:29
  • BitmapImage doesn't have PropertyItems. – Joe Feb 28 '18 at 13:34
  • @Joe *"BitmapImage doesn't have PropertyItems"* does not sound like a valid answer to *"What exactly are you trying to accomplish?"* – Manfred Radlwimmer Feb 28 '18 at 13:35
  • 1
    Small note: don't use `Image.FromFile(path)`. It is just a constructor of `Bitmap` returned as its less specific `Image` superclass. There's no reason not to use `new Bitmap(path)` instead. – Nyerguds Feb 28 '18 at 13:47
  • @Nyerguds It also keeps a lock on the file (as does the `Bitmap` constructor), I've always felt it better to read the file data in to a `Stream`, and then create the image from the `Stream`, to avoid that problem. – Bradley Uffner Feb 28 '18 at 14:05
  • Actually, Stream will not do either, unless you keep it open. I wrote an overview of image behaviours based on the source in [this answer](https://stackoverflow.com/a/48579791/395685). – Nyerguds Feb 28 '18 at 14:09
  • I'm trying to open a jpeg file having it's filename and read the propertyitems from the header. I got it working in my prototype in WinForm app, and now want to have the same in WPF app. – Joe Feb 28 '18 at 14:10
  • 1
    @Joe So your real question is "how to read image metadata using WPF". [Which is already answered on SO.](https://stackoverflow.com/q/688990/395685). – Nyerguds Feb 28 '18 at 14:19