473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Generate picture C#

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

Nov 17 '05 #1
4 10627
The following article was written for a Web Form but the code to create the
image should work in a Win Forms app too.
http://www.codeproject.com/aspnet/CaptchaImage.asp

--
Tim Wilson
..Net Compact Framework MVP

<Jo*********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 17 '05 #2
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

"Jo*********@gmail.com" wrote:
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

Nov 17 '05 #3

"Salvador" <Sa******@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
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
Nov 17 '05 #4
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_numb er.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.OutputS tream)
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
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chris Coho, Jr. | last post by:
Ok, I'll explain the whole problem because there may be several ways to solve this and hopefully someone knows one. What I'm doing is creating a specialty template editor, similar to say a corel...
0
by: Jay | last post by:
Hi I would like to generate rtf code of the picture/image programatically without using richtext box or clip board To be more specific I want to create .rtf file of image/pictures Need...
29
by: Lauren Wilson | last post by:
Does anyone know how the following info is extracted from the user's computer by a Front Page form? HTTP User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107...
2
by: Lyn | last post by:
I am trying to embed a picture into a Bound Object Frame (Me!Photograph) with the following code which is based on MS article http://support.microsoft.com/?id=158941: strPathname =...
6
by: John Ortt | last post by:
Hi there everyone, I have a part info form which has a faded image of our company logo as a background. I want to replace the faded image with a bright red warning image on items which have run...
4
by: Øyvind Isaksen | last post by:
Hello! Does anyone know about an ASP.NET thumbnail script that generate thumbnail with MAX quality? Have tested some scripts, but the thumbnail is not getting as good quality as I need. This...
3
by: Rajiv Das | last post by:
C# 2.0 XP SP2 In My Code, I am using HttpWebRequest to visit a particular URL. I am required to generate a snapshot image (if this request were made through say IE) and save as jpeg. About .1...
6
by: Jeff | last post by:
Hey (and thank you for reading my post) In visual web developer 2005 express edition I've created a simple website project.. At this website I want users who register to be able to upload a...
3
by: raghunadhs | last post by:
hi all! i have a picture box, in that picture box, there is a picture asume it as "pic1.bmp".. now i have made some changes in that picture. Now i want to load a picture to a image( my form...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.