Thursday, April 28, 2011

Regular expressions

Sometimes I required to do some tasks using regular expression. In this post I decided to collect my regular expressions:

First regular expression is to find images in the html and replace its src url:







static string ReplaceTag(string HTMLBody, string srcPath, string newPath)
{
Regex reImg = new Regex(@"]*>", RegexOptions.IgnoreCase);
Regex reHeight = new Regex(@"height=(?:(['""])(?(?:(?!\1).)*)\1(?[^\s>]+))", RegexOptions.IgnoreCase RegexOptions.Singleline);
Regex reWidth = new Regex(@"width=(?:(['""])(?(?:(?!\1).)*)\1(?[^\s>]+))", RegexOptions.IgnoreCase RegexOptions.Singleline);
Regex reSrc = new Regex(@"src=(?:(['""])(?(?:(?!\1).)*)\1(?[^\s>]+))", RegexOptions.IgnoreCase RegexOptions.Singleline);
string tmpHTMLBody = HTMLBody;
MatchCollection mc = reImg.Matches(HTMLBody);
foreach (Match mImg in mc)
{
Console.WriteLine(" img tag: {0}", mImg.Groups[0].Value);
string tmpImgTag = string.Empty;
string tmpOldImgSrc = string.Empty;
string tmpNewImgSrc = string.Empty;
tmpImgTag = mImg.Groups[0].Value;

if (reHeight.IsMatch(mImg.Groups[0].Value))
{
Match mHeight = reHeight.Match(mImg.Groups[0].Value);
Console.WriteLine(" height is: {0}", mHeight.Groups["height"].Value);
}
if (reWidth.IsMatch(mImg.Groups[0].Value))
{
Match mWidth = reWidth.Match(mImg.Groups[0].Value);
Console.WriteLine(" width is: {0}", mWidth.Groups["width"].Value);
}
if (reHeight.IsMatch(mImg.Groups[0].Value))
{
Match mSrc = reSrc.Match(mImg.Groups[0].Value);
tmpOldImgSrc = mSrc.Groups["src"].Value;
tmpNewImgSrc = tmpOldImgSrc.ToLower().Replace(srcPath.ToLower(), newPath.ToLower());
tmpHTMLBody = tmpHTMLBody.ToLower().Replace(tmpOldImgSrc.ToLower(), tmpNewImgSrc.ToLower());
Console.WriteLine(" src is: {0}", mSrc.Groups["src"].Value);
}
}
return tmpHTMLBody;
}

This Regular Expression to Clean Word to Html remove word classes and attributes:







static internal string CleanWordHtml(string html)
{
// start by completely removing all unwanted tags
html = Regex.Replace(html, @"<[/]?(fonth1h2h3h4h5h6bspanxmldelins[ovwxp]:\w+)[^>]*?>", "", RegexOptions.IgnoreCase);
// then run another pass over the html (twice), removing unwanted attributes
html = Regex.Replace(html, @"<([^>]*)(?:classlangstylesizeface[ovwxp]:\w+)=(?:'[^']*'""[^""]*""[^\s>]+)([^>]*)>", "<$1$2>", RegexOptions.IgnoreCase);
html = Regex.Replace(html, @"<([^>]*)(?:classlangstylesizeface[ovwxp]:\w+)=(?:'[^']*'""[^""]*""[^\s>]+)([^>]*)>", "<$1$2>", RegexOptions.IgnoreCase);
return html;
}

This Regular Expression to remove tags from Html:







static internal string StripHtml(string html, bool allowHarmlessTags)
{
if (html == null html == string.Empty)
return string.Empty;

if (allowHarmlessTags)
return System.Text.RegularExpressions.Regex.Replace(html, "", string.Empty);

string strippedHtml = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*>", string.Empty);
strippedHtml = HttpUtility.HtmlDecode(strippedHtml);
return strippedHtml;
}


Check that a string contains Arabic Characters using C#:





static internal bool hasArabic(string text)
{
Regex regex = new Regex(
"\\p{IsArabic}");
return regex.IsMatch(text);
}

Friday, April 8, 2011

How to run 32-bit UDL file on a 64-bit Operating System

Once we create an UDL File and run it on 64 Bit OS, it will list down all the 64 bit OLE DB providers installed on the machine. The reason behind for this is simple.
When you double click on a UDL file on a 64 bit machine, it’ll enumerate only the 64 bit OLE DB Providers and if we have the 32 bit Ole DB providers installed, we will not be able to find that in the enumerated list.
Create a udl file with the name test.udl under the path, C:\.
When we have created a UDL file on a 64 bit machine and try to open it, the following:
"C:\Program Files\Common Files\System\Ole DB\oledb32.dll",OpenDSLFile C:\test.udl Command will be called through C:\windows\system32\rundll32.exe.
Here both Oledb32.dll and rundll32.exe are 64 bit and will not enumerate the 32bit Dlls.
How to run the UDL which lists down the 32bit Dlls?
We can find the 32bit version of Oledb32.dll under the path:
C:\Program Files (x86)\Common Files\System\Ole DB
and 32 bit version of rundll32.exe under the path:
C:\WINDOWS\SysWOW64.
We’ll need to execute the command below from a command line or Start/Run :
C:\Windows\syswow64\rundll32.exe "C:\Program Files (x86)\Common Files\System\Ole DB\oledb32.dll",OpenDSLFile C:\test.udl
Check the paths of rundll32.exe and oledb32.dll while running this command

Saturday, January 22, 2011

Display Html in RDLC file

I want to display HTML in RDLC of reporting service. Report Viewer support html but with limited tags, I want to display HTML generated from rich text editor. After investigation I found the best solution is displaying it as image. Steps to do that:

1. Create class library convert HTML to image.
2. Use this class with report viewer.
3. Display report with asp net.

1. Create class library convert HTML to image
a. In the following url http://webapplication02.blogspot.com/2011/01/convert-html-to-image.html you find the way to create class library to convert html to image.
It is better to create image as bmp for better image quality but this will not work with excel.
You can convert image to another type but check its quality.
b. Need to add [assembly: AllowPartiallyTrustedCallers()] to trust class library for using by report viewer.

2. Use Class with report viewer:
a. Replace your html field in report with Image with properties:
i. Mime type image/bmp
ii. Type of field Database
iii. Field Expression =Code.GetHtmlImage(HtmlField)

b. Add this function to report:


Public Function GetHtmlImage(ByVal body as String) As Byte()
return htmlConv.getWebPageBytes(body, nothing, nothing)
End Function


c. Add Assembly reference to 3 dlls:
i. System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
ii. System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
iii. Your class library
d. Add class library ImageConv class and instance htmlConv
e. To build your solution with report reference to your class library copy your class library to folder:
($Program Files Path)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies

3. Display Report with ASPNET:


ReportViewer1.LocalReport.ExecuteReportInCurrentAppDomain(System.Reflection.Assembly.GetExecutingAssembly().Evidence);
ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain(Full Assembly name of your class libarary);

Also need to add Full Trust in Web.Config

Convert Html to Image

I searched for free open source csharp to convert html to image. I found solution using:

WebBrowser (namespace System.Windows.Forms) code below is sample of conversion:


static byte[] CaptureWebPageBytesP(string body, int? width, int? height)
{
byte[] data;
// create a hidden web browser, which will navigate to the page
using (WebBrowser web = new WebBrowser())
{
web.ScrollBarsEnabled = false; // we don't want scrollbars on our image
web.ScriptErrorsSuppressed = true; // don't let any errors shine through
web.Navigate("about:blank");
// wait until the page is fully loaded
while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
System.Windows.Forms.Application.DoEvents();
web.Document.Body.InnerHtml = body;

// set the size of our web browser to be the same size as the page
if (width == null)
width = web.Document.Body.ScrollRectangle.Width;
if (height == null)
height = web.Document.Body.ScrollRectangle.Height;
web.Width = width.Value;
web.Height = height.Value;
// a bitmap that we will draw to
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width.Value, height.Value))
{
// draw the web browser to the bitmap
web.DrawToBitmap(bmp, new Rectangle(web.Location.X, web.Location.Y, web.Width, web.Height));
// draw the web browser to the bitmap
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
EncoderParameter qualityParam = null;
EncoderParameters encoderParams = null;
try
{
ImageCodecInfo imageCodec = null;
//imageCodec = getEncoderInfo("image/jpeg");
imageCodec = getEncoderInfo("image/bmp");

// Encoder parameter for image quality
qualityParam = new EncoderParameter(Encoder.Quality, 100L);

encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = qualityParam;
bmp.Save(stream, imageCodec, encoderParams);
}
catch (Exception)
{
throw new Exception();
}
finally
{
if (encoderParams != null)
encoderParams.Dispose();
if (qualityParam != null)
qualityParam.Dispose();
}
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Position = 0;
data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
}
}
}
return data;
}

But I faced problem when I try to use it without Window Forms (console application, aspnet, or windows service) It gave me error cannot be instantiated because the current thread is not in a single-threaded. To solve this error created STAThread like code below:

public static byte[] CaptureWebPageBytes(string body, int? width, int? height)
{
bool bDone = false;
byte[] data = null;
DateTime startDate = DateTime.Now;
DateTime endDate = DateTime.Now;

//sta thread to allow intiate WebBrowser
var staThread = new Thread(delegate()
{
data = CaptureWebPageBytesP(body, width, height);
bDone = true;
});
staThread.SetApartmentState(ApartmentState.STA);
staThread.Start();
while (!bDone)
{
endDate = DateTime.Now;
TimeSpan tsp = endDate.Subtract(startDate);

Application.DoEvents();
if (tsp.Seconds > 50)
{
break;
}
}
staThread.Abort();
return data;
}


Wednesday, December 29, 2010

Site on IIS 7 gave me error 500

After setup .NET Framework 1.0 on windows 2008. When tring to browse any site, gave me error 500. Unable to load C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll.

After investigation I found solution to register Framework 4.0 again using command aspnet_regiis /i

Thursday, March 11, 2010

Fixing File Upload Size Limit in IIS 7

In the older IIS (IIS 6 or lower), adding the following code to in the web.config file in the web application’s folder Tallows a file upload of 2,000,000 kilobytes and it will time out after 100,000 seconds, or 27.8 hours.:

<httpruntime executiontimeout="100000" maxrequestlength="2000000">

To do this in IIS 7 on Windows 2008 Server add the following code to in the web.config file:

<security>
<requestfiltering>
<requestlimits maxallowedcontentlength="2000000000″>
</requestfiltering>
</security>

Thursday, August 13, 2009

.NET Image Manipulation programmatically

Sometimes developer need to do image manipulation pro grammatically:
  1. Resizing the image width/height.
  2. Change image format.
  3. Decrease image quality to decrease image size.
Code Below will help developers to do these tasks:
  1. We will declare variable for new image width, height, Quality(1-100), extension.
  2. Create Bitmap from physical file or stream.



  3. // Create a bitmap from the stream
    Bitmap oldImage = new Bitmap(filename);

  4. Initialize new bitmap using new width and height.


  5. // Initialize new bitmap using new width and height.
    Bitmap newImage = new Bitmap(Width,Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
  6. Initialize graphic from new image.
  7. Draw old image with new width and height to new image variable.

  8. Graphics g = Graphics.FromImage(newImage);
    g.DrawImage(oldImage,0,0,newImage.Width,newImage.Height);
  9. Before we save image we need get encoder of its new type.

  10. private ImageCodecInfo GetEncoderInfo(String mimeType)
    {
    ImageCodecInfo[] encoders;
    encoders = ImageCodecInfo.GetImageEncoders();
    for(int _iLoop = 0; _iLoop<encoders.Length; ++j)
    {
    if(encoders[_iLoop].MimeType == mimeType)
    {
    return encoders[_iLoop];
    }
    }
    return null;
    }
  11. Last step will save image with its new configurations:


  12. EncoderParameters imgParams = new EncoderParameters(1);
    imgParams.Param[0] = new
    EncoderParameter(System.Drawing.Imaging.Encoder.Quality,Quality);
    ImageCodecInfo ici = GetEncoderInfo(sMime);
    ImageCodecInfo newici = GetEncoderInfo(sNewMime);
    imgBitMap.Save(newstream, newici,imgParams);