public static void SetPropertyClosest(this WiaItemBase item, int propId, ref int value) { var prop = item.Properties[propId]; if (prop != null) { if (prop.Attributes.Flags.HasFlag(WiaPropertyFlags.List)) { int value2 = value; var choice = prop.Attributes.Values.OfType <int>().OrderBy(x => Math.Abs(x - value2)).Cast <int?>().FirstOrDefault(); if (choice != null) { prop.Value = choice.Value; value = choice.Value; } } else { // Not a list, try to set the property directly try { prop.Value = value; } catch (Exception e) { Log.ErrorException("Error setting property", e); } } } }
public static void SetPropertyRange(this WiaItemBase item, int propId, int value, int expectedMin, int expectedMax) { var prop = item.Properties[propId]; if (prop != null) { if (prop.Attributes.Flags.HasFlag(WiaPropertyFlags.Range)) { int expectedAbs = value - expectedMin; int expectedRange = expectedMax - expectedMin; int actualRange = prop.Attributes.Max - prop.Attributes.Min; int actualValue = expectedAbs * actualRange / expectedRange + prop.Attributes.Min; if (prop.Attributes.Step != 0) { actualValue -= actualValue % prop.Attributes.Step; } actualValue = Math.Min(actualValue, prop.Attributes.Max); actualValue = Math.Max(actualValue, prop.Attributes.Min); prop.Value = actualValue; } else { // Not a range, try to set the property directly try { prop.Value = value; } catch (Exception e) { Log.ErrorException("Error setting property", e); } } } }
public static void SetProperty(this WiaItemBase item, int propId, int value) { var prop = item.Properties[propId]; if (prop != null) { try { prop.Value = value; } catch (Exception e) { Log.ErrorException("Error setting property", e); } } }