29 Mar 2008
Recently I was trying to write a little C# function for cropping images. I expected it to be a quick 5 minute task but I ended up spending a huge amount of time getting it to work correctly. I kept getting a very stubborn and mysterious error - “Invalid Parameter Used” - whenever I tried to save my newly cropped image by doing bitmap.Save(). I tried various suggestions I found on forums and blogs to no avail.
Finally I figured out what the problem was. I was encapulatning the Bitmap and Graphics objects inside a “using” statement. So the Bitmap object was being prematurely disposed before I returned it back to the caller.
So in the end my solution looked like this. I just god rid of the “usings”.
public Bitmap CropImage(Image image, Rectangle cropRect)
{
Bitmap bitmap = new Bitmap(cropRect.Width, cropRect.Height, PixelFormat.Format24bppRgb);
bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.DrawImage(image, 0, 0, cropRect, GraphicsUnit.Pixel);
return bitmap;
}
Usage -
Bitmap croppedBmp = CropImage(”~/uploads/test.jpg”, new Rectangle(x, y, width, height));
croppedBmp.Save();
Moral of the story: If you are getting this error while calling Bitmap.Save() then make sure you are not disposing your Bitmap object prematurely. Hope this helps.
12 Mar 2008
SharpZipLib is a great open source library for handeling all kinds of gzip/zip compression/decompression. More Info - http://www.icsharpcode.net/OpenSource/SharpZipLib/
In the following example I’m passing the HtmlInputFile object directly into the ZipInputStream to decompress the PostedFile and save its contents on the server.
.
.
using System.IO;
using ICSharpCode.SharpZipLib.Zip
.
.
private void UnzipAndSave(HtmlInputFile objFileUpload)
{
ZipInputStream s = new ZipInputStream(objFileUpload.PostedFile.InputStream);
ZipEntry theEntry;
string virtualPath = "~/uploads/";
string fileName = string.Empty;
string fileExtension = string.Empty;
string fileSize = string.Empty;
while ((theEntry = s.GetNextEntry()) != null)
{
fileName = Path.GetFileName(theEntry.Name);
fileExtension = Path.GetExtension(fileName);
if (!string.IsNullOrEmpty(fileName))
{
try
{
FileStream streamWriter = File.Create(Server.MapPath(virtualPath + fileName));
int size = 2048;
byte[] data = new byte[2048];
do
{
size = s.Read(data, 0, data.Length);
streamWriter.Write(data, 0, size);
} while (size > 0);
fileSize = Convert.ToDecimal(streamWriter.Length / 1024).ToString() + ” KB”;
streamWriter.Close();
//Add custom code here to add each file to the DB, etc.
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
s.Close();
}
12 Mar 2008
In this case I’m resizing the video and converting it to FLV format. For more ffmpeg commandline options - http://ffmpeg.mplayerhq.hu/ffmpeg-doc.html
private void ConvertVideo(string srcURL, string destURL)
{
string ffmpegURL = "~/project/tools/ffmpeg.exe";
DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(Server.MapPath(ffmpegURL)));
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Server.MapPath(ffmpegURL);
startInfo.Arguments = string.Format("-i \"{0}\" -s 368x216 -aspect 1.7777 \"{1}\"", srcURL, destURL);
startInfo.WorkingDirectory = directoryInfo.FullName;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;
using (Process process = new Process())
{
process.StartInfo = startInfo;
try
{
process.Start();
StreamReader standardOutput = process.StandardOutput;
StreamWriter standardInput = process.StandardInput;
StreamReader standardError = process.StandardError;
process.WaitForExit();
lblError.Text = standardError.ReadToEnd();
lblOutput.Text = standardOutput.ReadToEnd();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
16 Feb 2008
Until recently I wasn’t entirely familiar with the concepts of Event Driven Programming, Event Bubbling, etc. Being a .NET developer I’ve been exposed to events and delegates but I never really understood the concept in its entirety. Somehow I had a hard time readily finding good sources online focusing on event driven programming especially using ASP.NET/C#. But I had to get myself more familiarized with the concept since I need to use a lot of that in my current project. So after doing some digging I found couple of good links which explain the concept quite well -
Hope that helps.
02 Feb 2008
Very good article. Covers all the following topics:
- ASP.NET Pipeline optimization
- ASP.NET Process configuration optimization
- Things you must do for ASP.NET before going live
- Content Delivery Network
- Caching AJAX calls on browser
- Making best use of Browser Cache
- On demand progressive UI loading for fast smooth experience
- Optimize ASP.NET 2.0 Profile provider
- How to query ASP.NET 2.0 Membership tables without bringing down the site
- Prevent Denial of Service (DOS) attack
http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx