CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    25

    Question WIA Image Processing Help

    I've struggled with the limited csharp examples and documentation for WIA for a while now, but I finally have it scanning from an ADF scanner (I will be posting finished code soon as a reference for others wanting to code WIA).

    What I'm now stuck with is using the Image Processing. I feel like I am so close yet it's just out of my grasp.

    Here is the code I've been currently trying:

    Code:
     WIA.ImageProcess IP = new ImageProcessClass();
                    
                    foreach (FilterInfo F in IP.FilterInfos)
                    {
                    	
                    	if (F.Name == "Convert")
                    	{
                    		IP.Filters.Add(F.FilterID,0);
                    		
                    		foreach (Property Prp in IP.Filters[1].Properties)
                    		{
                    			
                    			if (Prp.Name == "Compression")
                    			{
                    				//set jpeg compression for image
                    				
                    				IP.Filters[1].Properties[Prp] = 50;
                    			}
                    		}
                    		
                    		
                    	}
                    }
    Unfortunately the line IP.Filters[1].Properties[Prp] = 50; fails with errors:

    Argument '1': cannot convert from 'WIA.Property' to 'ref object' (CS1503)
    The best overloaded method match for 'WIA.IProperties.this[ref object]' has some invalid arguments (CS1502)

    Obviously I'm not doing it the right way for the properties but the examples on the net are all for VB and I can't seem to convert them to C# (I'm also fairly new to C#)

    This is the VB example for creating a filter:

    Code:
    Dim Img 'As ImageFile
    Dim IP 'As ImageProcess
    
    Set Img = CreateObject("WIA.ImageFile")
    Set IP = CreateObject("WIA.ImageProcess")
    
    Img.LoadFile "C:\WINDOWS\Web\Wallpaper\Bliss.bmp"
    
    IP.Filters.Add IP.FilterInfos("Convert").FilterID
    IP.Filters(1).Properties("FormatID").Value = wiaFormatJPEG
    IP.Filters(1).Properties("Quality").Value = 5
    
    Set Img = IP.Apply(Img)
    
    Img.SaveFile "C:\WINDOWS\Web\Wallpaper\BlissCompressed.jpg"
    Is anyone able to shed some light on how I can set the properties for the ImageFilter?

  2. #2
    Join Date
    Nov 2003
    Posts
    2,185

    Re: WIA Image Processing Help

    IP.Filters[1].Properties[Prp] = 50 is completely wrong.

    OR you should loop through the properties using a for-loop so you are able to use:

    IP.Filters[1].Properties[i] = 50

    OR you should use the reference to the property (and I assume you also need a .Value or something like that):

    Prp.Value = 50

  3. #3
    Join Date
    Oct 2007
    Posts
    25

    Re: WIA Image Processing Help

    Thank you, yes I know my test was completely wrong. The problem is I cannot work out what value to insert for IP.Filters[1].Properties[] as it does not accept an int or long or anything like that. It is looking for an object. If I type the first [ bracket SharpDevelop shows the following message indicating what it's looking for:

    WIA.Property WIA.IProperties.this[ref object Index]

    It does not accept a number value and I was assuming it might accept a "WIA.Property" object... unfortunately it doesn't or not in the way I'm trying.

    Any other suggestions?

  4. #4
    Join Date
    Nov 2003
    Posts
    2,185

    Re: WIA Image Processing Help

    Since it uses an object, you can put anything in it.

    Check the original documentation what value to set for each object.

    For example, you can set a property like this (from the example code you gave):


    Code:
    WIA.ImageProcess IP = new ImageProcessClass();
    
    foreach (FilterInfo F in IP.FilterInfos)
    {
        if (F.Name == "Convert")
        {
            IP.Filters.Add(F.FilterID,0);
                    		
            foreach (Property Prp in IP.Filters[1].Properties)
            {
                // Switch properties
                switch (prp.Name.ToLower())
                {
                    case "compression":
                        //set jpeg compression for image				
                        Prp.Value = 50;
                        break;
                }                	
            }
        }
    }

  5. #5
    Join Date
    Oct 2007
    Posts
    25

    Re: WIA Image Processing Help

    Thank you for helping, and yes I did try that. This is what frustrated me the most. The example does not match the practice.

    Prp does not have a ".Value" property. I also cannot put anything in the place for "object". A string gives me an error, and int gives me an error, so does everything else I tried.

    I've tried my best with searching and given up. It seems that it may be that with WIA Automation 2.0 one cannot set values, only read them, although I cannot confirm this in any documentation. I know that this is the case with the WIA Scripting Model 1.0.

    I have decided to make my own image processing functions to do what I need instead.

  6. #6
    Join Date
    Nov 2003
    Posts
    2,185

    Re: WIA Image Processing Help

    If it was a read-only field, the compiler would tell you, so this isn't a read-only field.

    The compiler cannot cast a string to an object, you have to do this yourself.

    Prp.Value = (object)50;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured