Generate picture C#

  • Thread starter Thread starter John.Arthur
  • Start date Start date
J

John.Arthur

Hello,

I need to create picture containing number that I will write in the
picture. I need it to show it to the users of my application and they
will write the number in a textBox, so this is to prevent automatic
activities with my application and to keep bots away.
But I have no idea how can I create a picture containing number or
text.

Can someone help please?
Thanks
 
Hi,

Ok, I will explain the hard way, maybe there is an easy way that I don't know.

Create a bitmap image using the System.Drawing like this:
Bitmap NewBitmap = new Bitmap(widht, height, pixelformat)

Have a function to translate numbers to pixels like:
private static Bitmap NumbersToPixel (byte number, Bitmap currentBitmap)
{
switch (number)
{
case 1:
currentBitmap.SetPixel(x,y,color);
}

I recommend you to use lines like the digital clocks, so you have 7 lines to
combine.

Then you can use your bitmap in your image control.

Hope this helps
Salva
 
Salvador said:
Hi,

Ok, I will explain the hard way, maybe there is an easy way that I don't
know.

Create a bitmap image using the System.Drawing like this:
Bitmap NewBitmap = new Bitmap(widht, height, pixelformat)

Have a function to translate numbers to pixels like:
private static Bitmap NumbersToPixel (byte number, Bitmap currentBitmap)
{
switch (number)
{
case 1:
currentBitmap.SetPixel(x,y,color);
}

Ok, that's the hard way? Then what about this (which will create the image
and allow for the image to be auto-generated/shown to the user w/o saving
the image to the hard disk.


Create class that Implements IHttpModule.

The class will render the image to a memory stream then WriteTo the
Response.OutputStream.
Add an event handler for OnBeginRequest using AddHandler inside of the
Init() method of this class.
In OnBeginRequest handler, check the sender.Request.Path to see if it's the
same path you set in the ImageUrl of the asp.net image below.

Add an asp.net image to the web form and set the ImageUrl to be that of some
name that you will check inside your OnBeginRequest event handler.

In Web.Config, you'll need to add an <httpModules> section.

There are some tweaks you will need to do, but that is an advanced way to do
this. If you have trouble, let me know. I have an example of it, but can't
send it, don' t have the time to package it all up to post it.

If I remember, I will tonight :)

Mythran
 
Oh well, was able to do it after all :)

Create ImageHttpModule class file and insert the following class
declaration:

Public Class ImageHttpModule
Implements IHttpModule


Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
' MustOverride Method.
End Sub

Public Sub Init(ByVal context As System.Web.HttpApplication) Implements
System.Web.IHttpModule.Init
AddHandler context.BeginRequest, AddressOf Me.OnBeginRequest
End Sub

Public Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim context As HttpApplication = DirectCast(sender, HttpApplication)

If context.Request.Path.ToLower().IndexOf("image_number.aspx") < 0
Return
End If

' Should really generate the random number and store in a database,
' then do the fetch on that number in the Try...Catch below,
' if the number is not in the database, then generate one and
' store in the database.
Dim num As Integer = New Random().Next(10000, 99999)

' Build the image.
Dim memStream As IO.MemoryStream
Try
memStream = RenderImage(num)
memStream.WriteTo(context.Context.Response.OutputStream)
memStream.Close()
memStream = Nothing ' Prevents the Close() call in Finally.

context.Context.ClearError()
context.Response.ContentType = "image/gif"
context.Response.StatusCode = 200
context.Response.End()
Catch
context.Response.StatusCode = 500
context.Response.End()
Finally
If Not memStream Is Nothing
memStream.Close()
End If
End Try
End Sub

Private Function RenderImage(ByVal Number As Integer) As IO.MemoryStream
Dim canvas As Graphics = Graphics.FromImage(New Bitmap(1, 1))
Dim size As SizeF
Dim numberFont As Font = New Font(New FontFamily("Verdana"), 12)

size = canvas.MeasureString( _
CStr(Number), _
numberFont _
)

size.Width += 10
size.Height += 10

Dim image As New Bitmap(size.Width, size.Height,
Imaging.PixelFormat.Format24bppRgb)

canvas = Graphics.FromImage(image)

Try
canvas.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
canvas.TextRenderingHint =
Drawing.Text.TextRenderingHint.SingleBitPerPixel

' Draw background.
Dim backRect As Rectangle = _
New Rectangle(0, 0, image.Width, image.Height)
canvas.Clear(Color.Black)

Dim brush As Brush = New SolidBrush(Color.White)

canvas.DrawString(CStr(Number), numberFont, brush, 5, 5)

Dim memStream As IO.MemoryStream = New IO.MemoryStream()
image.Save(memStream, Imaging.ImageFormat.Gif)
Return memStream
Finally
image.Dispose()
canvas.Dispose()
End Try
End Function

End Class


In the web form code-behind:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Me.IsPostBack
imgNumber.ImageUrl = "image_number.aspx"
End If
End Sub

In web.config before </system.web>:

<httpModules>
<add name="PictureImage" type="PictureSample.ImageHttpModule,
PictureSample" />
</httpModules>



Think that's it, hope it works for you :)

Mythran
 
Back
Top