Click here to Skip to main content
16,006,594 members
Home / Discussions / C#
   

C#

 
GeneralRe: Converting Pixels to 100/Inch Pin
Pain_Elemental5-Jul-04 2:19
Pain_Elemental5-Jul-04 2:19 
GeneralRe: Converting Pixels to 100/Inch Pin
Pain_Elemental5-Jul-04 3:53
Pain_Elemental5-Jul-04 3:53 
GeneralRe: Converting Pixels to 100/Inch Pin
exhaulted5-Jul-04 4:21
exhaulted5-Jul-04 4:21 
GeneralRe: Converting Pixels to 100/Inch Pin
Heath Stewart5-Jul-04 5:38
protectorHeath Stewart5-Jul-04 5:38 
GeneralRe: Converting Pixels to 100/Inch Pin
exhaulted5-Jul-04 6:06
exhaulted5-Jul-04 6:06 
GeneralRe: Converting Pixels to 100/Inch Pin
Heath Stewart5-Jul-04 6:27
protectorHeath Stewart5-Jul-04 6:27 
GeneralRe: Converting Pixels to 100/Inch Pin
exhaulted5-Jul-04 23:50
exhaulted5-Jul-04 23:50 
GeneralRe: Converting Pixels to 100/Inch Pin
Heath Stewart6-Jul-04 4:21
protectorHeath Stewart6-Jul-04 4:21 
The error suggest that you need to define the field, but in this case the field name should be twipsPerPixelX, which is already defined but never used. The proper code is:
public sealed class Display
{
  static Display()
  {
    SystemEvents.DisplaySettingsChanged += new EventHandler(Update);
  }
  private Display() {} // Prevent instantiation

  [DllImport("gdi32.dll")]
  private static extern IntPtr GetDeviceCaps(IntPtr hdc, IntPtr index);

  [DllImport("user32.dll")]
  private static extern IntPtr GetDC(IntPtr hwnd);

  [DllImport("user32.dll")]
  private static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);

  private static void Update(object sender, EventArgs e)
  {
    Setup(true);
  }
  private static void Setup(bool force)
  {
    if (initialized && !force) return;
    twipsPerPixelX  = twipsPerPixelY = 0; // Initial values
    IntPtr hdc = GetDC(IntPtr.Zero);
    try
    {
      if (!hdc.Equals(IntPtr.Zero))
      {
        IntPtr value;
        value = GetDeviceCaps(hdc, new IntPtr(88)); // LOGPIXELSX
        twipsPerPixelX = 1440 / value.ToInt32();
        value = GetDeviceCaps(hdc, new IntPtr(90)); // LOGPIXELSY
        twipsPerPixelY = 1440 / value.ToInt32();
        ReleaseDC(IntPtr.Zero, hdc);
      }
    }
    catch {}
    if (twipsPerPixelX == 0) twipsPerPixelX = 15; // Default for 96 DPI
    if (twipsPerPixelY == 0) twipsPerPixelY = 15; // Default for 96 DPI
    initialized = true;
  }
  private static bool initialized;
  private static int twipsPerPixelX;
  private static int twipsPerPixelY;
  public static int TwipsPerPixelX
  {
    get
    {
      Setup(false);
      return twipsPerPixelX;
    }
  }
  public static int TwipsPerPixelY
  {
    get
    {
      Setup(false);
      return twipsPerPixelY;
    }
  }
  public static int FromPixelsX(int x, ScaleMode mode)
  {
    switch (mode)
    {
      case ScaleMode.Points:
        return PixelsToTwipsX(x) / 20;
      case ScaleMode.Inches:
        return PixelsToTwipsX(x) / 1440;
      default:
        throw new ArgumentException("Invalid scale mode.", "mode");
    }
  }
  public static int FromPixelsY(int y, ScaleMode mode)
  {
    switch (mode)
    {
      case ScaleMode.Points:
        return PixelsToTwipsY(y) / 20;
      case ScaleMode.Inches:
        return PixelsToTwipsY(y) / 1440;
      default:
        throw new ArgumentException("Invalid scale mode.", "mode");
    }
  }
  public static int ToPixelsX(int x, ScaleMode mode)
  {
    switch (mode)
    {
      case ScaleMode.Points:
        return TwipsToPixelsX(x * 20);
      case ScaleMode.Inches:
        return TwipsToPixelsX(x * 1440);
      default:
        throw new ArgumentException("Invalid scale mode.", "mode");
    }
  }
  public static int ToPixelsY(int y, ScaleMode mode)
  {
    switch (mode)
    {
      case ScaleMode.Points:
        return TwipsToPixelsY(y * 20);
      case ScaleMode.Inches:
        return TwipsToPixelsY(y * 1440);
      default:
        throw new ArgumentException("Invalid scale mode.", "mode");
    }
  }
  public static int PixelsToTwipsX(int x)
  {
    Setup(false);
    return x * twipsPerPixelX;
  }
  public static int PixelsToTwipsY(int y)
  {
    Setup(false);
    return y * twipsPerPixelY;
  }
  public static int TwipsToPixelsX(int x)
  {
    Setup(false);
    return x / twipsPerPixelX;
  }
  public static int TwipsToPixelsY(int y)
  {
    Setup(false);
    return y / twipsPerPixelY;
  }
}

// A couple of examples
public enum ScaleMode
{
  Points,
  Inches
}


 

Microsoft MVP, Visual C#
My Articles
GeneralRay Picking Pin
ghho5-Jul-04 0:04
ghho5-Jul-04 0:04 
Generalenum and inheritance Pin
Anonymous4-Jul-04 23:39
Anonymous4-Jul-04 23:39 
GeneralRe: enum and inheritance Pin
hatim_ali5-Jul-04 2:12
hatim_ali5-Jul-04 2:12 
GeneralRe: enum and inheritance Pin
Anonymous5-Jul-04 3:50
Anonymous5-Jul-04 3:50 
GeneralRe: enum and inheritance Pin
Heath Stewart5-Jul-04 5:40
protectorHeath Stewart5-Jul-04 5:40 
GeneralRe: enum and inheritance Pin
leppie5-Jul-04 7:16
leppie5-Jul-04 7:16 
GeneralRe: enum and inheritance Pin
Anonymous5-Jul-04 22:58
Anonymous5-Jul-04 22:58 
GeneralGet Url from IE Pin
olreit4-Jul-04 22:57
olreit4-Jul-04 22:57 
GeneralRe: Get Url from IE Pin
eggie56-Jul-04 13:24
eggie56-Jul-04 13:24 
GeneralCasting String to Long Pin
hatim_ali4-Jul-04 22:52
hatim_ali4-Jul-04 22:52 
GeneralRe: Casting String to Long Pin
Stefan Troschuetz4-Jul-04 23:05
Stefan Troschuetz4-Jul-04 23:05 
GeneralRe: Casting String to Long Pin
hatim_ali4-Jul-04 23:24
hatim_ali4-Jul-04 23:24 
GeneralTime Validation Pin
dabuskol4-Jul-04 22:36
dabuskol4-Jul-04 22:36 
GeneralRe: Time Validation Pin
Colin Angus Mackay5-Jul-04 1:22
Colin Angus Mackay5-Jul-04 1:22 
GeneralRe: Time Validation Pin
dabuskol5-Jul-04 1:56
dabuskol5-Jul-04 1:56 
GeneralRe: Time Validation Pin
Colin Angus Mackay5-Jul-04 2:15
Colin Angus Mackay5-Jul-04 2:15 
Questionhow can i take lively image from a webcam? Pin
turgaytr4-Jul-04 21:21
turgaytr4-Jul-04 21:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.