/// <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 destDC1 = 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(); //destDC1 = 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); //SafeNativeMethods.TransparentBlt(destDC1, destBounds.X, destBounds.Y, destBounds.Width, destBounds.Height, destDC, sourceBounds.X, sourceBounds.Y, sourceBounds.Width, sourceBounds.Height, rgbBG); } 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; } //if (destDC1 != IntPtr.Zero) //{ // g.ReleaseHdc(destDC1); // destDC1 = IntPtr.Zero; //} } }
private void DrawFunction(PaintEventArgs e) { //Define font //Define string string drawText = "ABC"; //Define XY Point pt = new Point(0, 0); //Define anti bool antiAliasing = false; //Define Smoothing FontSmoothing smoothType = FontSmoothing.Sharp; //Fonts.DrawText(e.Graphics, font, "A", new Point(0, 0), false, FontSmoothing.Sharp); // Panel handle IntPtr destDC = IntPtr.Zero; IntPtr hOldObject = IntPtr.Zero; IntPtr dcMemery = IntPtr.Zero; IntPtr pbitmap = IntPtr.Zero; //IntPtr pTemp = IntPtr.Zero; Graphics g = e.Graphics; NativeStructs.RECT destBounds = new NativeStructs.RECT(); destBounds.left = 0; destBounds.top = 0; destBounds.bottom = 20; destBounds.right = 20; destDC = g.GetHdc(); //pTemp = SafeNativeMethods.CreateCompatibleDC(destDC); dcMemery = SafeNativeMethods.CreateCompatibleDC(destDC); // Font Font font = new Font("宋体", 10.0f, FontStyle.Regular, GraphicsUnit.Pixel); IntPtr hFont = IntPtr.Zero; hFont = Fonts.CreateFontObject(font, antiAliasing); SafeNativeMethods.SelectObject(dcMemery, hFont); pbitmap = SafeNativeMethods.CreateCompatibleBitmap(destDC, 20, 20); SafeNativeMethods.SelectObject(dcMemery, pbitmap); NativeStructs.RGBQUAD rgb = new NativeStructs.RGBQUAD(); rgb.rgbBlue = 255; NativeStructs.RGBQUAD rgbBG = new NativeStructs.RGBQUAD(); rgbBG.rgbBlue = 0; rgbBG.rgbRed = 0; rgbBG.rgbGreen = 0; //Graphics gSource = new Graphics(); //gSource. //IntPtr gPtr = gSource.GetHdc(); //gPtr = dcMemery; //gSource SafeNativeMethods.SetTextColor(dcMemery, rgb); SafeNativeMethods.SetBkColor(dcMemery, rgbBG); SafeNativeMethods.DrawTextW(dcMemery, drawText, drawText.Length, ref destBounds, NativeConstants.DT_CENTER); //SafeNativeMethods.TransparentBlt(pTemp, destBounds.left, destBounds.top, destBounds.right, destBounds.bottom, dcMemery, destBounds.left, destBounds.top, destBounds.right, destBounds.bottom, 0); SafeNativeMethods.StretchBlt(destDC, 0, 0, 20 * 10, 20 * 10, dcMemery, 0, 0, 20, 20, NativeConstants.SRCCOPY); //dcMemery. //pbitmap = bit.GetHbitmap(); //dcMemery = SafeNativeMethods.CreateCompatibleBitmap(pdc, 20, 20); //SafeNativeMethods. //hFont = CreateFontObject(font, antiAliasing); //hOldObject = SafeNativeMethods.SelectObject(pdc, hFont); //SafeNativeMethods.CreateCompatibleDC(pdc); //uint result1 = SafeNativeMethods.StretchBlt( // 0, // 0, // 20, // 20, // pdc, // 0, // 0, // 20 * 1000, // 20 * 1000, // NativeConstants.SRCCOPY); }