2010-01-15 25 views
47

Bất kỳ ai cũng có thể hướng dẫn cách tạo hình ảnh từ văn bản đầu vào. Hình ảnh có thể có bất kỳ phần mở rộng nào không quan trọng.Cách tạo hình ảnh từ văn bản khi đang bay tại thời gian chạy

+0

Bạn có nghĩa là một hình ảnh giống như bạn chụp được không? Chắc chắn, * một số * định dạng/tiện ích mở rộng sẽ tốt hơn các định dạng khác. – pavium

+0

Bạn muốn nhập loại văn bản nào? –

+0

Không có ảnh chụp màn hình chúng tôi có hộp văn bản đầu vào và chúng tôi đang sử dụng C# – Ravia

Trả lời

123

Ok, giả sử bạn muốn vẽ một chuỗi trên một hình ảnh trong C#, bạn sẽ cần phải sử dụng không gian tên System.Drawing đây:

private Image DrawText(String text, Font font, Color textColor, Color backColor) 
{ 
    //first, create a dummy bitmap just to get a graphics object 
    Image img = new Bitmap(1, 1); 
    Graphics drawing = Graphics.FromImage(img); 

    //measure the string to see how big the image needs to be 
    SizeF textSize = drawing.MeasureString(text, font); 

    //free up the dummy image and old graphics object 
    img.Dispose(); 
    drawing.Dispose(); 

    //create a new image of the right size 
    img = new Bitmap((int) textSize.Width, (int)textSize.Height); 

    drawing = Graphics.FromImage(img); 

    //paint the background 
    drawing.Clear(backColor); 

    //create a brush for the text 
    Brush textBrush = new SolidBrush(textColor); 

    drawing.DrawString(text, font, textBrush, 0, 0); 

    drawing.Save(); 

    textBrush.Dispose(); 
    drawing.Dispose(); 

    return img; 

} 

mã này sẽ đo chuỗi đầu tiên, một sau đó tạo một hình ảnh có kích thước chính xác.

Nếu bạn muốn lưu lại hàm này, chỉ cần gọi phương thức Lưu của hình ảnh đã trả về.

+6

Dòng "Hình ảnh img = bitmap mới (0, 0);", không hoạt động: bạn không thể tạo Hình ảnh có kích thước 0. Thay đổi nó thành "bitmap mới (1, 1)", nó hoạt động. – neminem

+3

Nếu bạn thêm 'drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;' trước 'drawing.DrawString (văn bản, phông chữ, textBrush, 0, 0); dòng', bạn sẽ nhận được văn bản chống răng cưa mượt mà hơn . – LoneBunny

3

sử dụng imagemagick cho rendering văn bản trên ảnh (trên máy chủ)

Kể từ khi bạn đang ở trong C# bạn cũng có thể sử dụng các lớp Net cho bitmap và font thao tác trực tiếp (với các lớp học như: System.Drawing.BitmapSystem.Drawing.Graphics)

3

Tôi vừa dịch phương pháp này được đề cập trong phương thức này là answer thành phương pháp VB.NET. Có lẽ điều này giúp một ai đó.

Public Function DrawText(ByVal text As String, ByRef font As Font, ByRef textColor As Color, ByRef backColor As Color) As Image 
    ' first, create a dummy bitmap just to get a graphics object 
    Dim img As Image = New Bitmap(1, 1) 
    Dim drawing As Graphics = Graphics.FromImage(img) 

    ' measure the string to see how big the image needs to be 
    Dim textSize As SizeF = drawing.MeasureString(Text, Font) 

    ' free up the dummy image and old graphics object 
    img.Dispose() 
    drawing.Dispose() 

    ' create a new image of the right size 
    img = New Bitmap(CType(textSize.Width, Integer), CType(textSize.Height, Integer)) 

    drawing = Graphics.FromImage(img) 

    ' paint the background 
    drawing.Clear(BackColor) 

    ' create a brush for the text 
    Dim textBrush As Brush = New SolidBrush(textColor) 

    drawing.DrawString(text, font, textBrush, 0, 0) 

    drawing.Save() 

    textBrush.Dispose() 
    drawing.Dispose() 

    Return img 

End Function 

Chỉnh sửa: Sửa lỗi chính tả.

+0

Cảm ơn Freddy, bạn đã cứu tôi rất nhiều năng lượng. – BedfordNYGuy

1

F# phiên bản:


open System.Drawing 

let drawText text font textColor backColor = 
    let size = 
     use dummyImg = new Bitmap(1, 1) 
     use drawing = Graphics.FromImage(dummyImg) 
     drawing.MeasureString(text, font) 
    let img = new Bitmap((int size.Width), (int size.Height)) 
    use drawing = Graphics.FromImage(img) 
    use textBrush = new SolidBrush(textColor) 
    do 
     drawing.Clear(backColor) 
     drawing.DrawString(text, font, textBrush, PointF()) 
     drawing.Save() |> ignore 
    img 
3

Cảm ơn Kazar. Một chút cải thiện của câu trả lời trước đó để sử dụng USING cho việc xử lý các đối tượng hình ảnh/đồ họa sau khi sử dụng và giới thiệu kích thước tối thiểu param

private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor) { 
     return DrawTextImage(currencyCode, font, textColor, backColor, Size.Empty); 
    } 
    private Image DrawTextImage(String currencyCode, Font font, Color textColor, Color backColor, Size minSize) { 
     //first, create a dummy bitmap just to get a graphics object 
     SizeF textSize; 
     using (Image img = new Bitmap(1, 1)) { 
      using (Graphics drawing = Graphics.FromImage(img)) { 
       //measure the string to see how big the image needs to be 
       textSize = drawing.MeasureString(currencyCode, font); 
       if (!minSize.IsEmpty) { 
        textSize.Width = textSize.Width > minSize.Width ? textSize.Width : minSize.Width; 
        textSize.Height = textSize.Height > minSize.Height ? textSize.Height : minSize.Height; 
       } 
      } 
     } 

     //create a new image of the right size 
     Image retImg = new Bitmap((int)textSize.Width, (int)textSize.Height); 
     using (var drawing = Graphics.FromImage(retImg)) { 
      //paint the background 
      drawing.Clear(backColor); 

      //create a brush for the text 
      using (Brush textBrush = new SolidBrush(textColor)) { 
       drawing.DrawString(currencyCode, font, textBrush, 0, 0); 
       drawing.Save(); 
      } 
     } 
     return retImg; 
    } 
Các vấn đề liên quan