Helpex - Trao đổi & giúp đỡ Đăng nhập
39

Tôi có một vấn đề sau đây. Tôi muốn tạo một số đồ họa ở dạng ảnh bitmap như biểu mẫu trái phiếu

tôi có thể viết văn bản bằng hình ảnh
nhưng tôi sẽ viết nhiều văn bản hơn ở nhiều vị trí khác nhau

Bitmap a = new Bitmap(@"path\picture.bmp");

using(Graphics g = Graphics.FromImage(a))
{
    g.DrawString(....); // requires font, brush etc
}

Làm cách nào để tôi có thể viết và lưu văn bản cũng như viết một văn bản khác trong hình ảnh đã lưu.

39 hữu ích 2 bình luận 90k xem chia sẻ
87

Để vẽ nhiều chuỗi, hãy gọi graphics.DrawStringnhiều lần. Bạn có thể chỉ định vị trí của chuỗi được vẽ. Ví dụ này, chúng tôi sẽ vẽ hai chuỗi "Xin chào", "Từ" ("Xin chào" màu xanh lam phía trước "Word" màu đỏ):

string firstText = "Hello";
string secondText = "World";

PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);

string imageFilePath = @"path\picture.bmp"
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file

using(Graphics graphics = Graphics.FromImage(bitmap))
{
    using (Font arialFont =  new Font("Arial", 10))
    {
        graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
        graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
    }
}

bitmap.Save(imageFilePath);//save the image file

Chỉnh sửa: "Tôi Thêm tải và lưu mã".

Bạn có thể mở tệp bitmap bất kỳ lúc nào Image.FromFilevà vẽ văn bản mới trên đó bằng đoạn mã trên. và sau đó lưu tệp hình ảnhbitmap.Save

87 hữu ích 5 bình luận chia sẻ
6

Đây là một ví dụ về cuộc gọi đến Graphics.DrawString, được lấy từ đây :

g.DrawString("My\nText", new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));

Nó rõ ràng dựa vào việc Tahomacài đặt một phông chữ được gọi là .

Các Brusheslớp đã nhiều built-in bàn chải.

Xem thêm, trang MSDN cho Graphics.DrawString.

6 hữu ích 0 bình luận chia sẻ
4

Để lưu các thay đổi vào cùng một tệp, tôi phải kết hợp câu trả lời của Jalal Said và câu trả lời của NSGaga về câu hỏi này . Bạn cần tạo một đối tượng Bitmap mới dựa trên đối tượng cũ, loại bỏ đối tượng Bitmap cũ , sau đó lưu bằng đối tượng mới:

string firstText = "Hello";
string secondText = "World";

PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);

string imageFilePath = @"path\picture.bmp";

Bitmap newBitmap;
using (var bitmap = (Bitmap)Image.FromFile(imageFilePath))//load the image file
{
    using(Graphics graphics = Graphics.FromImage(bitmap))
    {
        using (Font arialFont =  new Font("Arial", 10))
        {
            graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
            graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
        }
    }
    newBitmap = new Bitmap(bitmap);
}

newBitmap.Save(imageFilePath);//save the image file
newBitmap.Dispose();
4 hữu ích 0 bình luận chia sẻ
0
    public string imageFilePath = null;
    public string textOnImage = null;

    public Image baseImage;
    public Image modifiedImage;

    public int xcoOrdinate = 0;
    public int ycoOrdinate = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void buttonLoadImage_Click(object sender, EventArgs e)
    {
        try
        {
            OpenFileDialog uploadfileDialog = new OpenFileDialog();
            uploadfileDialog.Filter = "All Files (*.*)|*.*";
            uploadfileDialog.Multiselect = false;

            if (uploadfileDialog.ShowDialog() == DialogResult.OK)
            {
                imageFilePath = uploadfileDialog.FileName;
            }

            baseImage = Image.FromFile(imageFilePath);
            modifiedImage = (Image)baseImage.Clone();
            pictureBoxToShowPic.Image = baseImage;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }            
    }

    public void paint()
    {
        try
        {
            Graphics g = Graphics.FromImage(modifiedImage);
            using (Font myfont = new Font("Arial", 14))
            {
                var format = new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                };

                g.DrawString(textOnImage, myfont, Brushes.Black, new Point(xcoOrdinate, ycoOrdinate), format);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }
    }

    private void buttonAddText_Click(object sender, EventArgs e)
    {
        try
        {
            textOnImage = textBoxWriteText.Text;
            paint();
            pictureBoxToShowPic.Image = modifiedImage;
            pictureBoxToShowPic.Refresh();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }
    }

    private void pictureBoxToShowPic_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        try
        {
            xcoOrdinate = e.X;
            ycoOrdinate = e.Y;
        }            
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }
    }

    private void buttonSaveImage_Click(object sender, EventArgs e)
    {
        try
        {
            SaveFileDialog savefileDialog = new SaveFileDialog();

            savefileDialog.Filter = "Images|*.jpg ; *.png ; *.bmp";

            if (savefileDialog.ShowDialog() == DialogResult.OK)
            {
                imageFilePath = savefileDialog.FileName;
            }

            modifiedImage.Save(imageFilePath);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Source + " : " + ex.Message);
        }
    }
0 hữu ích 1 bình luận chia sẻ
-7

Nếu ai đó gặp sự cố với dòng mã này:

using(Graphics graphics = Graphics.FromImage(bitmap))

Giải pháp là :

Bitmap bitmap = (Bitmap)**System.Drawing.Image.FromFile**(@"C:\Documents and Settings\", true);
-7 hữu ích 1 bình luận chia sẻ
loading
Không tìm thấy câu trả lời bạn tìm kiếm? Duyệt qua các câu hỏi được gắn thẻ c# image text draw , hoặc hỏi câu hỏi của bạn.

Có thể bạn quan tâm

loading