GDI 文本失真放大,欢迎高手指教,分数不够可以开新贴追加,如果有例子,可以给例子也行,欢迎大家都参与讨论,
1、我要的是放大后,字号不变,放大失真的效果,我用C#代码没有实现
2、我一个朋友用C++实现了,希望大家帮我转换成C#的
3、效果图如下:
4、C++代码
void CFontDlg::Test(int n)
{
CWnd * wnd = GetDlgItem(IDC_STATIC_FONT);
if(wnd)
{
CDC *pdc = wnd->GetDC();
CDC dcMemery;
//dcMemory -> Graphics
dcMemery.CreateCompatibleDC( (CDC*)pdc );
//pbitmap -> Image
CBitmap * pbitmap = new CBitmap;
pbitmap->CreateCompatibleBitmap( pdc, 20, 20);
//SelectObject -> FromImage
dcMemery.SelectObject(pbitmap);
//Graphics.DrawString
dcMemery.FloodFill(0, 0,RGB(25,3,25) );
dcMemery.TextOut(3,3,"A");
//Streth
pdc->StretchBlt( 0, 0, 20*n, 20*n,
&dcMemery, 0, 0, 20, 20, SRCCOPY );
ReleaseDC(pdc);
delete pbitmap;
}
}
void CFontDlg::OnSelchangeCombo1()
{
int n = m_Type.GetCurSel();
m_nSize = n+2;
CWnd * wnd = GetDlgItem(IDC_STATIC_FONT);
wnd->Invalidate(TRUE);
}
5、C#代码:
[解决办法]
Graphic.DrawImageUnscaled()类似BitBlt()
Graphic.DrawImage()类似StretchBlt()
- C# code
public partial class Form1 : Form { Bitmap memBmp = new Bitmap(150, 50); public Form1() { // 准备一个字符串图形 // using (Graphics g = Graphics.FromImage(memBmp)) { g.DrawString("Hello world", new Font("Times New Roman", 16), Brushes.Black, 10, 10); } } protected override void OnPaint(PaintEventArgs e) { using (Graphics g = e.Graphics) { // 原样画图 // g.DrawImageUnscaled(memBmp, 0, 0); // 放大,采用最近点取样,突出失真的效果 // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor; //<--- g.DrawImage(memBmp, new Rectangle(0, 50, 600, 200), new Rectangle(0, 0, 150, 50), GraphicsUnit.Pixel); //<--- } } }
[解决办法]
- C# code
public Bitmap CreateFontBmp(Font font, string text, float zoom) { if (string.IsNullOrEmpty(text)) return null; Size size = TextRenderer.MeasureText(text, font); Rectangle rect = new Rectangle(0,0,size.Width,size.Height); Bitmap bmp = new Bitmap(size.Width, size.Height); Graphics graphics = Graphics.FromImage(bmp); TextRenderer.DrawText( graphics, text, font, rect, SystemColors.ControlText); if (zoom > 0 && zoom != 1) { int width = (int)Math.Ceiling(size.Width * zoom); int height = (int)Math.Ceiling(size.Height * zoom); Rectangle tmpRect = new Rectangle(0,0,width,height); Bitmap tmpBmp = new Bitmap(width, height); graphics = Graphics.FromImage(tmpBmp); graphics.DrawImage( bmp, tmpRect, rect, GraphicsUnit.Pixel); graphics.Flush(); graphics.Dispose(); bmp.Dispose(); return tmpBmp; } graphics.Flush(); graphics.Dispose(); return bmp; }
[解决办法]
参考:
- C# code
Graphics g = this.pictureBox1.CreateGraphics(); g.SmoothingMode = SmoothingMode.HighQuality; GraphicsPath path = new GraphicsPath(); StringFormat strformat = new StringFormat(); strformat.Alignment = StringAlignment.Center; strformat.LineAlignment = StringAlignment.Center; path.AddString("轩", new FontFamily("黑体"), (int)this.Font.Style, 400f, new Point(250,250), strformat); g.FillPath(new SolidBrush(Color.Red), path);