Thursday, October 29, 2020
Beetle Blog
By: douglas - C# - 5/22/2008 3:56:27 AM

This code uploads an image to the server file system.  If the image is wider than a certain width it resizes the image.  The image is saved as a jpg.


        // Upload image
        // Make sure to create directory if needed.
        string savePath = Server.MapPath("~/UploadFolder/");

        // Create Directory if it doesn't exist
        if (!System.IO.Directory.Exists(savePath))

        // Declare some image variables
        System.Drawing.Image imgOriginal;  // Will hold the original image
        System.Drawing.Image imgNew;  // Will hold the new image
        double targetWidth = 100; // Default target width for new image
        double targetHeight = 100; // Default target height for new image

        // Get new width from config
            targetWidth = Convert.ToDouble(ConfigurationManager.AppSettings["MaxImageWidth"]);
            targetWidth = 100;

        //Create a new Image using the uploaded picture as a Stream
        imgOriginal = Bitmap.FromStream(ImageUpload.PostedFile.InputStream);

        // Work out a proportionate height from width
        if ((double)imgOriginal.Width < targetWidth)
            targetWidth = (double)imgOriginal.Width;
            targetHeight = (double)imgOriginal.Height;
            targetHeight = (double)imgOriginal.Height / ((double)imgOriginal.Width / targetWidth);

        // Now create the new image
        imgNew = new Bitmap(imgOriginal, (int)targetWidth, (int)targetHeight);

        // Save it to the file system as a jpg.
        imgNew.Save(savePath + "\\" + pAdID.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

By: douglas - C# - 4/28/2008 6:13:14 AM

The following will validate stringVariable to see if it matches an email pattern.

System.Text.RegularExpressions.Regex.IsMatch(stringVariable, "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

There are double \ due to the language being C#.


By: douglas - C# - 6/27/2003 2:11:00 AM

This code inserts a record into the database using a DataSet. Using this method you don't need to know SQL and have more control over how things are inserted. Good for adding or updating records.

By: douglas - C# - 10/3/2002 12:00:00 AM

Normally when you click on a link to a file the browser will try to read or open that file automatically. This code will force the browser to open the "Save As..." dialog box using a swf file as an example.

<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
  Response.ContentType = "application/x-shockwave-flash";
  Response.AppendHeader("content-disposition","attachment; filename=button1.swf");


By: douglas - C# - 8/9/2002 12:00:00 AM

This tech note shows how to compile a C# source file using the command line and tools provided with the .NET SDK

Compiling a source file into a library:
csc /t:library /out:bin/myLibraryAssembly.dll myCodeBehindFile.cs

Compiling a source file recursively into a library:
csc /t:library /out:bin/myLibraryAssembly.dll /recurse:*.cs

Compiling a source file into an exe:
csc /out:mySingleFileAssembly.exe myCodeBehindFile.cs

Note: You may need to set up path variables for you system if using cscresults in a "program not found error". The reason is because the system can't find the csc.exe program. Environment variables can be found in the control panel under "System". Your system path variable should include the paths to your compiler tools:
C:\Program Files\Microsoft.NET\FrameworkSDK\Bin\;
C:\Program Files\Microsoft Visual Studio .NET\Common7\IDE\;

If you don't have access to the system variables, you may be able to put the path variable in your user path variable.

You may have to restart your computer to complete the change.

By: douglas - C# - 6/17/2002 12:00:00 AM

This code snippet shows examples of DateTime and TimeSpan. It also shows how to subtract dates and timespans.

By: douglas - C# - 6/16/2002 12:00:00 AM
hyperlink1.attributes("onclick") = "'xyz.htm');"


Beetle Blog v0.2.2

Copyright © 1997 - 2020 All rights reserved.