English 中文(简体)
Calculate the bounding box of a GDI+ drawing
原标题:

I am drawing an image from MetaFile (emf) and then apply some rotation transformations to it all within the OnPaint of a UserControl. After applying those transformation how can I calculate the normal untransformed rectangular bounding box of this in screen coordinates? I need this to be able to resize the rotated image to the size of the UserControl.

protected override void OnPaint(PaintEventArgs e)
{
    // rotate around the center of this UserControl
    e.Graphics.TranslateTransform(this.Width / 2.0f, this.Height / 2.0f);
    e.Graphics.RotateTransform(this.Rotation);
    e.Graphics.TranslateTransform(this.Width / -2.0f, this.Height / -2.0f);

    // TODO: now scale so the image so it fits exactly into this UserControl

    // draw the image at the center of this UserControl
    float left = (this.Width - ResourceManager.MyDrawingMetaFile.Width) / 2.0f;
    float top = (this.Height - ResourceManager.MyDrawingMetaFile.Height) / 2.0f;
    e.Graphics.DrawImage(Resources.MyDrawingMetaFile, left, top);
}

The whole idea behind this is that I want to display rotated .emf File in a UserControl and have the emf drawing allways fill the available space in the UserControl. Maybe there is a better approach?

The fillmode/stretchmode I am after is Uniform and UniformToFill (like in WPF s Viewbox). The emf should not be distorted an in Uniform mode the emf completely fills the usercontrol at least in one dimension, nothing is cropped. In UniformToFill the emf filles the UserControl in both dimensions and if the aspectratios do not match, the emf is cropped in one dimension.

最佳回答

If I understand you correctly - you need to figure out how the rotation affects the bounding box of your image, so that you can scale it accordingly.

Then you can do like this:

  1. Stuff the four coordinates of your bounding box in a Point[].
  2. Set up a Matrix with your rotation (.RotateAt)
  3. Let the matrix transform the four points.
  4. Sort the four transformed X coordinates and compare the width of the new bounding box (pts[3].X - pts[0].X after sort).
  5. Now you know how to scale the width for a perfect fit.
  6. Repeat step 4 for the height as well.
问题回答

The way it would be done with GDI is :

BeginPath()
// Draw stuff
EndPath()
PathToRegion()
GetRgnBox()

GDI+ has equivalents - The GraphicsPath and Region classes can do the above





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签