|
|||||||||||||||||||||||||
|
|||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
ContentsIntroduction
This project presents the way to dynamically generate a picture with C# and
ASP.NET. Basically a browser points to your page and gets back a picture
grabbed on your webcam. Method
The first thing to do to get a picture from an ASP.NET page is to determine the
MIME file type that the file will handle. In our case the MIME type is JPEG. So
we define in the <%@ Page Language="c#" ContentType="image/jpeg" %>We also need to specify at the top of the page the namespace that will be used: <%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Net" %>
Then comes the code that will grab the image from the wbecam and returns it to
the client:
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Jpeg compression quality
short nQuality = 45;
//Shout a picture from my webcam
CAMSERVERLib.Camera cam = new CAMSERVERLib.CameraClass();
byte[] picture = (byte[])cam.GrabFrame( nQuality );
//Add the hour to the jpeg picture
MemoryStream ms = new MemoryStream( picture );
Bitmap bmp = new Bitmap( ms );
Graphics g = Graphics.FromImage( bmp );
string strDate = DateTime.Now.ToLongDateString() + " - " +
DateTime.Now.ToLongTimeString();
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
g.DrawString( strDate,
new Font( FontFamily.GenericSansSerif, 12 ),
new SolidBrush( Color.Black ),
new RectangleF( 1,1,320,240 ),
drawFormat
);
g.DrawString( strDate,
new Font( FontFamily.GenericSansSerif, 12 ),
new SolidBrush( Color.White ),
new RectangleF( 0,0,320,240 ),
drawFormat
);
//Get codecs
ImageCodecInfo[] icf = ImageCodecInfo.GetImageEncoders();
EncoderParameters encps = new EncoderParameters( 1 );
EncoderParameter encp = new EncoderParameter( System.Drawing.Imaging.Encoder.Quality,
(long) nQuality );
//Set quality
encps.Param[0] = encp;
bmp.Save( Response.OutputStream, icf[1], encps );
g.Dispose();
bmp.Dispose();
}
</script>
In the code there is nothing new, compared to the other articles. We get a jpeg
picture grabbed by the webcam using COM Interop. We add the hour and the little
difference is that the image stream is saved to Response.OutputStream
.
UsageYou may reference the ASP.NET page has a normal jpeg picture: <img src="http://XXX/WebcamPicture/WebForm1.aspx" width="320" height="240"/>
or in a ASP:Image
control.
ConclusionThis opens lots of possibilities in the creation of dynamic graphics. For example you may create dynamic charts linked to database values returned has a normal graphic in a presentation page. You may also make easily a visit counter for your pages... Known Issues
History
|
||||||||||||||||||||||||