从Vb/net代码生成图像文件. [英] Generating Image file from Vb/net code.

查看:75
本文介绍了从Vb/net代码生成图像文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从给定的字符串生成图像文件.我使用Google并获得了以下代码,但它给出了错误:

I want to generate a image file from a given string. I did Google and got following code but it is giving error:

Dim Img As Image, Grf As Graphics

        Dim Str As String = "THE STRING"

        Dim Fnt As New Font(Me.Font, FontStyle.Regular)

        'Set the properties of the font (Fnt), e.g. :

        Fnt = New Font(Fnt.FontFamily, 6) 'The size of the font will be small

        Dim TempBmp As New Bitmap(TextRenderer.MeasureText(Str, Fnt).Width, TextRenderer.MeasureText(Str, Fnt).Height)

        Grf = Graphics.FromImage(TempBmp)

        Grf.DrawString(Str, Fnt, Brushes.Black, 0, 0)

        Me.BackgroundImage = TempBmp

        Img.Save("d:\tst.bmp", Imaging.ImageFormat.Bmp) 'Bmp or any another images format

推荐答案

这对您来说是不好的好:报告错误,只字不提.您甚至没有提到这是编译器消息还是异常.你想浪费人的时间吗?

我可以看到一些问题:

变量Img未初始化;您只需声明它,以后再也不要触摸它,然后尝试调用Save.它与您尝试使用TempBmp无关.顺便说一句,检查一下:您不是要画黑底黑字吗?这样的文字可读性不高... :-)

此外,在任何情况下,像"d:\ tst.bmp"这样的硬编码路径名都不会有用.在您的情况下,"d:"可能存在或不存在(在我的系统之一上,甚至"C:"都不存在;这不是必须的),而在Windows 7上甚至可能不合法.所有路径名都应在运行时根据不同的数据计算.

—SA
This is not nice of you: report an error and tell not a word about it. You did not even mention if that is a compiler message or exception. Do you like to waste people''s time?

I can see some problems:

The variable Img is not initialized; you just declare it and later never touch it, and then try to call Save. It has nothing to do with your attempt to draw on TempBmp. By the way, check it up: aren''t you trying to draw black on black? Such text won''t be very well readable… :-)

Besides, there are no cases when hard-coded path names like your "d:\tst.bmp" can be useful. In your case, "d:" may or may not exist (on one of my systems, even "C:" does not exist; this is not a must), and on Windows 7 it might be not even legal. All path names should be calculated during run time, based on different data.

—SA


Kryukov是正确的,因为变量Img没有任何设置,因此它当然不会保存任何内容适当地.您可以使用创建的位图对象保存文件.这是我为其中一个应用找到并保存为jpg文件的方法:

Kryukov is correct in that the variable Img doesn''t have anything set to it, so of course it won''t save anything properly. You can save the file with the bitmap object that you have created. Here is a method that I found and tweeked for one of my apps to save a jpg file:

Private Sub CreateImage(ByVal strText As String, ByVal strFileName As String, _
                            ByVal inFont As Font, ByVal inForeColor As Color, _
                            ByVal inBackColor As Color)
        'Create a bitmap of the required size
        Dim Height As Integer = 400
        Dim Width As Integer = 620
        Dim objBitmap As New Bitmap(Width, Height)

        'Create a Graphics object using this Bitmap object
        Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)

        'RectangleF object defines where the text will be displayed in the specified area of the image
        Dim objRect As New RectangleF(5, 5, 610, 390)

        'Create two SolidBrush type objects
        Dim objBrushForeColor As New SolidBrush(inForeColor)
        Dim objBrushBackColor As New SolidBrush(inBackColor)

        'Draw rectangle using Graphics object and fill it with BackColor
        objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height)

        'Draw Text string on the specified rectangle using Graphics object.
        objGraphics.DrawString(strText, inFont, objBrushForeColor, objRect)

        'Save the image on your hard drive in the specified format in JPEG format
        objBitmap.Save(strFileName & ".JPG", ImageFormat.Jpeg)
        objBrushForeColor.Dispose()
        objBrushBackColor.Dispose()
        objBitmap.Dispose()
        objGraphics.Dispose()
End Sub



我的方法很难设置为创建400 x 620像素的图像,并且我将其另存为我的项目所需的jpg文件,但是至少这向您展示了保存的工作方式.您应该至少可以使用此代码来解决此问题.希望对您有所帮助.



My method is hard set to create a 400 x 620 pixel image and I save as a jpg file which was required for my project, but at least this shows you how the save is supposed to work. You should be able to use this code to at least get you past this issue. Hope it helps.


这篇关于从Vb/net代码生成图像文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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