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

本文关键字:文本 bmp | 更新日期: 2023-09-27 17:59:01

我正在尝试将文本键入.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;
    }

有人能帮我吗?

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

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;
}

这应该是你想要的