我写的关于实现字体失真缩放的函数,是调用API实现的
1、函数用到的API及常量枚举声明:
待续
这个就是一个文件,不过内部集成的代码很长,能有4000多行吧,是我从微软的开源产品Paint.NET直接拿过来的,大家都知道C#类型,跟API类型不一样,所以很多都需要进行转化才能使用,这个文件里包含了很多API的声明以及API变量类型与C#类型之间的转化,你把这个Copy过去直接就可以使用,但是里面的API不是所有的都集成了,如果你需要新增API,就从MSDN复制API函数过来,然后把类型改成C#对应的类型就行,具体要参加其他类型API的声明,我的程序需要这个文件的支持,如果大家需要的话,可以留邮件给我,我给你们发我测试的源码。
2、函数源码:
2.1字体缩放函数
#region Strech text by API
/// <summary>
/// Function: Strech text
/// Author : Jerry Xu
/// Date : 2008-8-2
/// </summary>
/// <param name="g">Graphics:destination graphics</param>
/// <param name="drawText">string:destination text</param>
/// <param name="font">Font</param>
/// <param name="sourceBounds">source rectangle</param>
/// <param name="antiAliasing">bool</param>
private static void StrechText(Graphics g, string drawText, Font font, Color foreColor, Color backColor, Rectangle sourceBounds, float zoom, bool antiAliasing)
{
//Define InPtr(point)
IntPtr destDC = IntPtr.Zero;
IntPtr dcMemery = IntPtr.Zero;
IntPtr pbitmap = IntPtr.Zero;
IntPtr hFont = IntPtr.Zero;
//Define destination bound
Rectangle destBounds;
try
{
//Destionation DC
destDC = g.GetHdc();
//Temp DC
dcMemery = SafeNativeMethods.CreateCompatibleDC(destDC);
//Init font
font = new Font("宋体", 14.0f, FontStyle.Regular, GraphicsUnit.Pixel);
//Set font to temp DC
hFont = font.ToHfont();
SafeNativeMethods.SelectObject(dcMemery, hFont);
//Create image and get image DC
pbitmap = SafeNativeMethods.CreateCompatibleBitmap(destDC, sourceBounds.Width, sourceBounds.Height);
//Set image DC to temp DC
SafeNativeMethods.SelectObject(dcMemery, pbitmap);
//Set fore color
NativeStructs.RGBQUAD rgb = new NativeStructs.RGBQUAD();
rgb.rgbBlue = foreColor.B;
rgb.rgbRed = foreColor.R;
rgb.rgbGreen = foreColor.G;
SafeNativeMethods.SetTextColor(dcMemery, rgb);
//Set back color
NativeStructs.RGBQUAD rgbBG = new NativeStructs.RGBQUAD();
rgbBG.rgbBlue = backColor.B;
rgbBG.rgbRed = backColor.R;
rgbBG.rgbGreen = backColor.G;
SafeNativeMethods.SetBkColor(dcMemery, rgbBG);
//Draw text to temp Rectangle
NativeStructs.RECT rect = ConvertRectangleToRECT(sourceBounds);
SafeNativeMethods.DrawTextW(dcMemery, drawText, drawText.Length, ref rect, NativeConstants.DT_CENTER);
//Zoom Rectangle
destBounds = new Rectangle(sourceBounds.X, sourceBounds.Y, (int)(sourceBounds.Width * zoom), (int)(sourceBounds.Height*zoom));
//Draw temp Rectangle to zoom Rectangle
SafeNativeMethods.StretchBlt(destDC, destBounds.X, destBounds.Y, destBounds.Width, destBounds.Height, dcMemery, sourceBounds.X, sourceBounds.Y, sourceBounds.Width, sourceBounds.Height, NativeConstants.SRCCOPY);
}
catch
{
}
finally
{
//Disponse resource
if (pbitmap != IntPtr.Zero)
{
SafeNativeMethods.SelectObject(dcMemery, pbitmap);
pbitmap = IntPtr.Zero;
}
if (dcMemery != IntPtr.Zero)
{
SafeNativeMethods.SelectObject(destDC, dcMemery);
dcMemery = IntPtr.Zero;
}
if (hFont != IntPtr.Zero)
{
SafeNativeMethods.DeleteObject(hFont);
hFont = IntPtr.Zero;
}
if (destDC != IntPtr.Zero)
{
g.ReleaseHdc(destDC);
destDC = IntPtr.Zero;
}
}
}
#endregion
2.2 字体缩放用到的Rectangle 转换函数
#region Convert Rectangle to NativeStructs.RECT
/// <summary>
/// Function: Convert Rectangle to NativeStructs.RECT
/// Author : Jerry Xu
/// Date : 2008-8-2
/// </summary>
/// <param name="source">Rectangle</param>
/// <returns>NativeStructs.RECT</returns>
private static NativeStructs.RECT ConvertRectangleToRECT(Rectangle source)
{
NativeStructs.RECT target = new NativeStructs.RECT();
target.left = source.Left;
target.top = source.Top;
target.bottom = source.Right;
target.right = source.Bottom;
return target;
}
#endregion
3、使用:
待续