/// <summary>
/// Returns new dimensions of the image's bounding rect according to its parameters
/// </summary>
/// <param name="pict">Image reference</param>
/// <param name="rect">Image bounding rect</param>
/// <param name="ppos">Image alignment</param>
/// <param name="picw">New image's width value</param>
/// <param name="pich">New image's height value</param>
/// <param name="xoff">Image's X offset</param>
/// <param name="yoff">Image's Y offset</param>
/// <returns>true if successfull otherwise false</returns>
public bool GetImageDim(Image pict, RectangleF rect, ImageAlign ppos, ref float picw, ref float pich , ref float xoff , ref float yoff)
{
bool bOk = false;
int xc = 0, yc = 0;
try
{
if ( rect.Equals(RectangleF.Empty))
return false;
picw = 0;
pich = 0;
xoff =0;
yoff =0;
Graphics g = System.Drawing.Graphics.FromHwnd(GetActiveWindow());
setTransforms(g);
// get image logical size in document coordinates
RectangleF sizeDev = RectangleF.FromLTRB(0, 0,
(float)pict.Size.Width / pict.HorizontalResolution * g.DpiX * g.PageScale,
(float)pict.Size.Height / pict.VerticalResolution * g.DpiY * g.PageScale);
RectangleF sizeDoc = deviceToDoc(g, sizeDev);
picw = sizeDoc.Width;
pich = sizeDoc.Height;
switch (ppos)
{
case ImageAlign.TopLeft:
xoff = rect.Left;
yoff = rect.Top;
break;
case ImageAlign.BottomLeft:
xoff = rect.Left;
yoff = rect.Bottom - pich;
break;
case ImageAlign.TopRight:
xoff = rect.Right - picw;
yoff = rect.Top;
break;
case ImageAlign.BottomRight:
xoff = rect.Right - picw;
yoff = rect.Bottom - pich;
break;
case ImageAlign.Center:
xoff = (rect.Right + rect.Left - picw) / 2;
yoff = (rect.Bottom + rect.Top - pich) / 2;
break;
case ImageAlign.TopCenter:
xoff = rect.X + rect.Width / 2 - picw / 2;
yoff = rect.Y;
break;
case ImageAlign.BottomCenter:
xoff = rect.X + rect.Width / 2 - picw / 2;
yoff = rect.Bottom - pich;
break;
case ImageAlign.MiddleLeft:
xoff = rect.X;
yoff = rect.Y + rect.Height / 2 - pich / 2;
break;
case ImageAlign.MiddleRight:
xoff = rect.Right - picw;
yoff = rect.Y + rect.Height / 2 - pich / 2;
break;
case ImageAlign.Fit:
{
float h = rect.Bottom - rect.Top;
float w = rect.Right - rect.Left;
if (h == 0) break;
float ratioCtrl = w / h;
float ratioPic = picw / pich;
if (ratioCtrl > ratioPic)
{
//stretch vertically
pich = (int)h;
picw = (int)(ratioPic * pich);
yoff = rect.Top;
xoff = (rect.Right + rect.Left - picw) / 2;
}
else
{
//stretch horizontally
picw = (int)w;
if (ratioPic == 0) break;
pich = (int)(picw / ratioPic);
xoff = rect.Left;
yoff = (rect.Bottom + rect.Top - pich) / 2;
}
}
break;
case ImageAlign.Stretch:
{
picw = rect.Right - rect.Left;
pich = rect.Bottom - rect.Top;
xoff = rect.Left; yoff = rect.Top;
}
break;
case ImageAlign.Tile:
{
xoff = rect.Left; yoff = rect.Top;
xc = (int)((rect.Right - rect.Left) / picw) + 1;
yc = (int)((rect.Bottom - rect.Top) / pich) + 1;
}
break;
default:
return false;
}
PointF center = new PointF(
rect.X + rect.Width / 2,
rect.Y + rect.Height / 2);
bOk = true;
}
catch ( Exception ex )
{
Trace.WriteLine(String.Format("{0} error {1}\n","SvgManager.GetImageDim",ex.Message));
bOk = false;
}
return bOk;
}