简体   繁体   English

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

[英]C# trying to type text on a .bmp

I'm trying to type text into a .bmp, normally the background of the textbox is black and i would like to change it to a .bmp. 我正在尝试将文本键入.bmp,通常文本框的背景为黑色,我想将其更改为.bmp。 The name of the .bmp is 205.bmp. .bmp的名称是205.bmp。 I found this code which i think sets the background of the Richtextbox to black. 我发现了这段代码,我认为它会将Richtextbox的背景设置为黑色。 How do I change it to work with a picture background ? 如何将其更改为与图片背景配合使用?

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

Can anyone help me? 谁能帮我?

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

That should do what you want 那应该做你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM