使用图片框作为画布并绘制文本 [英] Use picturebox as a canvas and draw text

查看:67
本文介绍了使用图片框作为画布并绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将PictureBox用作画布并在其上绘制一些文本并保存.

I want to use a PictureBox as a canvas and draw some text on it and save.

我编写了这段代码,但不确定即时通讯是否以正确的方式进行操作

I wrote this piece of code but I'm not sure if im doing this the correct way:

        Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        Graphics g = Graphics.FromImage(b);
        g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); // i used this code to make the background color white 
        g.DrawString("some text", new Font("Times New Roman", 20), new SolidBrush(Color.Red), new PointF(10, 10));
        pictureBox1.Image = b;

此代码效果很好,但是当我想更改图像的背景颜色时,必须重绘文本.

This code works well but when I want to change the background color of the image I have to redraw the text.

是否有一种无需重新绘制文本即可更改背景颜色的方法?

Is there a way to change the background color without having to redraw the text?

推荐答案

编写Paint程序很有趣,但是您需要针对所有或大部分所需的功能进行预先计划.

Writing a Paint program is a lot of fun, but you need to plan ahead for all or most of the features you want.

到目前为止,您拥有这些:

So far you have these:

  • 可以更改的背景
  • 一种通过在其上绘制文本来修改图像的方法
  • 需要将其全部保存到文件中

这里您还需要一些其他东西:

Here are a few more things you will need:

  • 其他工具,例如文本,线条,矩形等.
  • 多种颜色和宽度的笔
  • 一种撤消一个或多个步骤的方法

以下是一些值得拥有的东西:

Here are few thing that are nice to have:

  • 一种使用鼠标进行绘制和定位的方法
  • 其他类型的背景,例如画布或香水纸
  • 具有一定透明度的绘画能力
  • 重做功能(*)
  • 旋转和缩放(***)
  • 级别(*****)

某些事情比其他事情难( * )或难得多( *** ),但是所有在您决定时会变得困难修补它们为时已晚..

Some things are harder (*) or a lot harder (***) than others, but all get hard when you decide to patch them on too late..

请阅读

Do read this post (starting at 'actually') about PictureBoxes, which explain how it is the ideal choice for a Paint program.

您的原始代码和您的问题有以下问题:

Your original piece of code and your question have these problems:

  • 您似乎认为重复任何操作(例如重画文字)都是错误的.它不是.Windows一直在重画大量的东西.
  • 您混合了两个确实应该分开的任务.
  • 您还没有参数化任何内容,最值得注意的是,文本的绘制应使用多个变量:

  • You seem to think that repeating anything, like redrawing the text is wrong. It is not. Windows redraws huge numbers of things all the time..
  • You have mixed two of the tasks which really should be separate.
  • You have not parametrizied anything, most notably the drawing of the text should use several variables:

  • 字体
  • 画笔
  • 位置
  • 文本本身

绘制直线或矩形后同样如此.

The same will be true once you draw lines or rectangles..

因此,以下是如何正确处理的提示:

So here are the hints how do get it right:

  • 使用 Picturebox BackgroundColor 和/或 BackgroundImage 动态更改背景!

  • Use the BackgroundColor and/or the BackgroundImage of the Picturebox to dynamically change the background!

收集所有内容以绘制 List< someDrawActionclass>

通过将其绘制成到Picturebox的 Image

Combine all drawings by drawing it into he Picturebox's Image

使用 Paint 事件在移动鼠标时绘制诸如临时矩形或直线之类的辅助内容.在 MouseUp 上,将其添加到列表中.

Use the Paint event to draw supporting things like the temporary rectangle or line while moving the mouse. On MouseUp you add it to the list..

所以,到最后,让我们修复您的代码..

So, coming to the end, let's fix your code..:

您可以使用以下功能设置背景:

You set the backgound with a function like this:

void setBackground(Color col, string paperFile)
{
   if (paperFile == "") pictureBox1.BackColor = col;
   else pictureBox1.BackgroundImage = Image.FromFile(paperFile);
}

您可以这样称呼它: setBackground(Color.White,");

要在 PictureBox Image 中将一段文本绘制到,首先请确保您拥有一个文本:

To draw a piece of text into the Image of the PictureBox, first make sure you have one:

void newCanvas()
{
   Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
   pictureBox1.Image = bmp;
}

现在您可以编写一个函数来编写文本.您真的不应该对任何设置进行硬编码,更不用说文本了!这只是一个快速且非常肮脏的示例..:

Now you can write a function to write text. You really should not hard-code any of the settings, let alone the text! This is just a quick and very dirty example..:

void drawText()
{
    using (Font font = new Font("Arial", 24f))
    using (Graphics G = Graphics.FromImage(pictureBox1.Image))
    {
        // no anti-aliasing, please
        G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
        G.DrawString("Hello World", font, Brushes.Orange, 123f, 234f);
    }
    pictureBox1.Invalidate();
}

请参见此处

See here and here for a few remarks on how to create a drawAction class to store all the things your drawing is made up from..!

最后一点是如何保存 PictureBox 的所有层:

The last point is how to save all layers of the PictureBox:

void saveImage(string filename)
{
    using (Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width,
                                   pictureBox1.ClientSize.Height))
    {
        pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
        bmp.Save("yourFileName.png", ImageFormat.Png);
    }
}

这篇关于使用图片框作为画布并绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆