473,782 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
+ 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 10653
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*********@gm ail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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.S etPixel(x,y,col or);
}

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*********@gm ail.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******@discu ssions.microsof t.com> wrote in message
news:4C******** *************** ***********@mic rosoft.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.S etPixel(x,y,col or);
}


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.Output Stream.
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.IHtt pModule.Dispose
' MustOverride Method.
End Sub

Public Sub Init(ByVal context As System.Web.Http Application) Implements
System.Web.IHtt pModule.Init
AddHandler context.BeginRe quest, AddressOf Me.OnBeginReque st
End Sub

Public Sub OnBeginRequest( ByVal sender As Object, ByVal e As EventArgs)
Dim context As HttpApplication = DirectCast(send er, 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(1 0000, 99999)

' Build the image.
Dim memStream As IO.MemoryStream
Try
memStream = RenderImage(num )
memStream.Write To(context.Cont ext.Response.Ou tputStream)
memStream.Close ()
memStream = Nothing ' Prevents the Close() call in Finally.

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

Private Function RenderImage(ByV al Number As Integer) As IO.MemoryStream
Dim canvas As Graphics = Graphics.FromIm age(New Bitmap(1, 1))
Dim size As SizeF
Dim numberFont As Font = New Font(New FontFamily("Ver dana"), 12)

size = canvas.MeasureS tring( _
CStr(Number), _
numberFont _
)

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

Dim image As New Bitmap(size.Wid th, size.Height,
Imaging.PixelFo rmat.Format24bp pRgb)

canvas = Graphics.FromIm age(image)

Try
canvas.Smoothin gMode = Drawing2D.Smoot hingMode.HighQu ality
canvas.TextRend eringHint =
Drawing.Text.Te xtRenderingHint .SingleBitPerPi xel

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

Dim brush As Brush = New SolidBrush(Colo r.White)

canvas.DrawStri ng(CStr(Number) , numberFont, brush, 5, 5)

Dim memStream As IO.MemoryStream = New IO.MemoryStream ()
image.Save(memS tream, Imaging.ImageFo rmat.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.EventArg s) Handles MyBase.Load
If Not Me.IsPostBack
imgNumber.Image Url = "image_number.a spx"
End If
End Sub

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

<httpModules>
<add name="PictureIm age" type="PictureSa mple.ImageHttpM odule,
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
9511
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 draw (but for specific uses). What I need to be able to do is import graphics and text and then move them around the background until they are where I want them and then export it out as an image file. The problem is that I need to be able to...
0
1770
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 help in above regard..... Thanx in advance.
29
3757
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 Firefox/1.0 I only ask because I believe I could use the same info as part of a scheme to generate a unique (or at least less common) serialized id code for the user's computer as part of a software locking and activation system. If I had a DLL...
2
8973
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 = "C:\photo.bmp" Me!Photograph.Class = "Paint.Picture" Me!Photograph.OLETypeAllowed = acOLEEmbedded Me!Photograph.SourceDoc = strPathname Me!Photograph.Action = acOLECreateEmbed
6
9482
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 out of purchasing cover. I am nearly there, the only problem is that the code below only changes the image background for text and combo-box backgrounds, it doesn't apply it to the whole form.
4
1998
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 is the best result I have got so far: Original picture: http://www.kromogkubikk.no/custom/artimgs/bakgard.jpg Thumb, 128px:
3
1191
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 million such requests are made in a cycle. What's the possible solution ?
6
8125
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 picture of themselves to their profile... I admit that I'm a newbie... but this is how I understand this:
3
2593
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 consists of a picture box and a image also). like image1.picture=loadPicture(picture1.picture) ... but i know that this statement is invalid.... how can i do it? actually.. yesterday i learned how to make changes on a picture and how to save a...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.