C#尝试在.bmp上键入文本

时间:2015-06-23 08:30:00

标签: c# text bmp

我试图将文本输入到.bmp中,通常文本框的背景是黑色的,我想将其更改为.bmp。 .bmp的名称是205.bmp。 我发现这个代码我认为将Richtextbox的背景设置为黑色。如何更改它以使用图片背景?

    public Bitmap RtbToBitmap(RichTextBox rtb, int width, int height)
    {
        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        using (Graphics gr = Graphics.FromImage(bmp))
        {
            gr.Clear(Color.Black);
            System.IntPtr hDC = gr.GetHdc(); // 屏幕做为画源
            FORMATRANGE fmtRange;
            RECT rect;
            int fromAPI;
            float inch = 100F;
            rect.Top = 10; 
            rect.Left = 10;
            rect.Bottom = (int)(bmp.Height + (bmp.Height * (bmp.HorizontalResolution / 100)) * inch);
            rect.Right = bmp.Width * 15;
            fmtRange.chrg.cpMin = 0;
            fmtRange.chrg.cpMax = -1;
            fmtRange.hdc = hDC;
            fmtRange.hdcTarget = hDC;
            fmtRange.rc = rect;
            fmtRange.rcPage = rect;
            int wParam = 1;
            System.IntPtr lParam = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
            System.Runtime.InteropServices.Marshal.StructureToPtr(fmtRange, lParam, false);
            fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, lParam);
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(lParam);
            fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, new IntPtr(0));
            gr.ReleaseHdc(hDC);
        }
        return bmp;
    }

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

public Bitmap RtbToBitmap(RichTextBox rtb)
{
    Bitmap bmp = // Load your Image here
    using (Graphics gr = Graphics.FromImage(bmp))
    {
        // gr.Clear(Color.Black); // Don't clear it with black
        System.IntPtr hDC = gr.GetHdc(); // 屏幕做为画源
        FORMATRANGE fmtRange;
        RECT rect;
        int fromAPI;
        float inch = 100F;
        rect.Top = 10; 
        rect.Left = 10;
        rect.Bottom = (int)(bmp.Height + (bmp.Height * (bmp.HorizontalResolution / 100)) * inch);
        rect.Right = bmp.Width * 15;
        fmtRange.chrg.cpMin = 0;
        fmtRange.chrg.cpMax = -1;
        fmtRange.hdc = hDC;
        fmtRange.hdcTarget = hDC;
        fmtRange.rc = rect;
        fmtRange.rcPage = rect;
        int wParam = 1;
        System.IntPtr lParam = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));
        System.Runtime.InteropServices.Marshal.StructureToPtr(fmtRange, lParam, false);
        fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, lParam);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(lParam);
        fromAPI = SendMessage(rtb.Handle, EM_FORMATRANGE, wParam, new IntPtr(0));
        gr.ReleaseHdc(hDC);
    }
    return bmp;
}

那应该做你想做的事