Thursday, June 14, 2012

How to covert string to image in MVC action?

        public FileContentResult ReturnImage()
        {
            Bitmap img = CreateBitmapImage("Render string to an image.");
            return File(Image2Byte(img), "image/tiff");
        }

        private byte[] Image2Byte(Bitmap img)
        {
            byte[] byteArray = new byte[0];
            using (MemoryStream stream = new MemoryStream())
            {
                img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
                stream.Close();
                byteArray = stream.ToArray();
            }
            return byteArray;
        }

        private Bitmap CreateBitmapImage(string sImageText)
        {
            Bitmap objBmpImage = new Bitmap(1, 1);
            int intWidth = 0;
            int intHeight = 0;
            // Create the Font object for the image text drawing.

            Font objFont = new Font("Arial", 12, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);

            // Create a graphics object to measure the text's width and height.

            Graphics objGraphics = Graphics.FromImage(objBmpImage);

            // This is where the bitmap size is determined.

            intWidth = (int)objGraphics.MeasureString(sImageText, objFont).Width;

            intHeight = (int)objGraphics.MeasureString(sImageText, objFont).Height;

            // Create the bmpImage again with the correct size for the text and font.

            objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));

            // Add the colors to the new bitmap.

            objGraphics = Graphics.FromImage(objBmpImage);

            // Set Background color

            objGraphics.Clear(Color.White);

            objGraphics.SmoothingMode = SmoothingMode.AntiAlias;

            objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

            objGraphics.DrawString(sImageText, objFont, new SolidBrush(Color.Red), 0, 0);

            objGraphics.Flush();

            return (objBmpImage);

        }

iTextSgarp, ASP.NET Create PDF From GridView

http://csharpdotnetfreak.blogspot.com/2012/06/aspnet-create-pdf-from-gridview.html

Control the execution order of your filters in ASP.NET Web API

http://www.strathweb.com/2012/06/control-the-execution-order-of-your-filters-in-asp-net-web-api/

 public class BaseActionFilterAttribute : ActionFilterAttribute, IBaseAttribute
{
    public int Position { get; set; }
   public BaseActionFilterAttribute()
   {
     this.Position = 0;   }
   public BaseActionFilterAttribute(int positon)
   {
      this.Position = positon;
   }
}