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

C#尝试在.bmp上键入文本,c#,text,bmp,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.Format24bpp

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

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;
}
那应该是你想要的


这应该是您想要的

如下所示:位图bmp=(@Application.StartupPath+“205.bmp”)
Bitmap bmp
是类型为
Bitmap
的变量,但
(@Application.StartupPath+“205.bmp”)
是字符串。您必须从保存的图像创建位图!查看一下位图的不同构造函数是的,我找到了。。。你的帖子真的很有用。。。非常感谢。如下所示:位图bmp=(@Application.StartupPath+“205.bmp”)
Bitmap bmp
是类型为
Bitmap
的变量,但
(@Application.StartupPath+“205.bmp”)
是字符串。您必须从保存的图像创建位图!查看一下位图的不同构造函数是的,我找到了。。。你的帖子真的很有用。。。非常感谢。