Sunday, February 3, 2008

How to Convert String to Image in C#

I had worked on a project that required a conversion from string the user would enter to an image, this can be used as easy Captcha too. Here is a method of how to Convert String to Image:

public static void DrawText(Color foreColor, Color backColor, string fontName, int fontSize, string txt, int height, int width, string imagePath)
{
    Bitmap img = new Bitmap(height, width);
    Graphics Gimg = Graphics.FromImage(img);
    Font imgFont = new Font(fontName, fontSize);
    PointF imgPoint = new PointF(5, 5);
    SolidBrush bForeColor = new SolidBrush(foreColor);
    SolidBrush bBackColor = new SolidBrush(backColor);

    Gimg.FillRectangle(bBackColor, 0, 0, width, height);
    Gimg.DrawString(txt, imgFont, bForeColor, imgPoint);
    img.Save(imagePath, ImageFormat.Jpeg);
}

Friday, February 1, 2008

How to Get XPath to an XML Node

There are a lot of XSL templates out there that get a human readable path to a node. But none that cover the case when there are more than one node of the same element at the same level.

In order to solve this problem you can use the following code:


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes">
    <xsl:template match="@*|node()">
      <xsl:copy>
        <xsl:tagibute name="position">
          <xsl:call-template name="xpath.position">
            <xsl:apply-templates select="@*|node()"></xsl:apply-templates>
          </xsl:call-template>
        </xsl:tagibute>
      </xsl:copy>
    </xsl:template>

    <xsl:template name="xpath.position">
     <xsl:param name="node" select="."></xsl:param>
     <xsl:param name="path" select="''"></xsl:param>
     <xsl:variable name="next.path">
      <xsl:text>*[position()=</xsl:text>
      <xsl:value-of select="$node/count(preceding-sibling::*)+1">
       <xsl:text>]</xsl:text>
       <xsl:if test="$path != ''">/</xsl:if>
       <xsl:value-of select="$path"></xsl:value-of>
      </xsl:value-of>
     </xsl:variable>

   <xsl:choose>
    <xsl:when test="$node/parent::*">
      <xsl:call-template name="xpath.position">
        <xsl:with-param name="node" select="$node/parent::*"></xsl:with-param>
          <xsl:with-param name="path" select="$next.path"></xsl:with-param>
      </xsl:call-template>
    </xsl:when>
 
    <xsl:otherwise>
      <xsl:text>/</xsl:text>
        <xsl:value-of select="$next.path"></xsl:value-of>
      </xsl:otherwise>
    </xsl:choose>
   </xsl:template>
  </xsl:output>
</xsl:stylesheet>

Hire Me

Follow me on Facebook

Follow me

Do you find this Blog helpful?

Follow by Email

About Me

My Photo
Expert Senior Software Developer

Microsoft Business Card