This is my Code
This code is working fine for the .jpeg/.Gif format
but i want to save image to .png format
this code gives Error A generic error occurred in GDI+
public override void ExecuteResult(ControllerContext context)
{
PageHeading c = new PageHeading();
//Captcha c = new Captcha();
c.Text = _captchaText;
c.Width = 685;
c.Height = 50;
c.FamilyName = "FlemishScript BT";
HttpContextBase cb = context.HttpContext;
//MemoryStream MemStream = new MemoryStream();
cb.Response.Clear();
cb.Response.ContentType = "image/png";
c.Image.Save(cb.Response.OutputStream, System.Drawing.Imaging.ImageFormat.png);
//c.Image.Save(cb.Response.OutputStream, ImageFormat.png);
c.Dispose();
}
Loading
Manoj TambePosted Sep 28, 2011, 6:40 AM
1)-----------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using WeddingWindow.Controllers;
namespace WeddingWindow.Models
{
public class PageHeading:PreviewSiteController
{
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;
private static Random random = new Random();
public string FamilyName
{
get { return familyName; }
set { familyName = value; }
}
public string Text
{
get { return this.text; }
set { text = value; }
}
public Bitmap Image
{
get
{
if (!string.IsNullOrEmpty(text) && height > 0 && width > 0)
GenerateImage();
return this.image;
}
}
public int Width
{
get { return this.width; }
set { width = value; }
}
public int Height
{
get { return this.height; }
set { height = value; }
}
public PageHeading()
{
}
~PageHeading()
{
Dispose(false);
}
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
this.image.Dispose();
}
private void SetDimensions(int width, int height)
{
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
this.width = width;
this.height = height;
}
private void SetFamilyName(string familyName)
{
try
{
Font font = new Font(this.familyName, 16F);
this.familyName = familyName;
font.Dispose();
}
catch
{
this.familyName = FontFamily.GenericSerif.Name;
}
}
public void GenerateImage()
{
//---- WITHOUT an image for the background ------
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(this.width, this.height,PixelFormat.Format32bppPArgb);
// Create a graphics object for drawing.
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
// MemoryStream memorystream = new System.IO.MemoryStream();
Rectangle rect = new Rectangle(0, 0, this.width, this.height);
// Fill in the background.
//HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, ColorTranslator.FromHtml("#607f20"), ColorTranslator.FromHtml("#ffea00"));
//HatchBrush hatchBrush = new HatchBrush(HatchStyle.Trellis, ColorTranslator.FromHtml("#ffffff"), ColorTranslator.FromHtml("#607f20"));
//HatchBrush hatchBrush = new HatchBrush(HatchStyle.Vertical, Color.FromArgb(114,172,236), Color.FromArgb(161,214,255));
HatchBrush hatchBrush = new HatchBrush(HatchStyle.Cross, Color.FromArgb(255, 255, 255), Color.FromArgb(255, 255, 255));
g.FillRectangle(hatchBrush, rect);
// Set up the text font.
SizeF size;
float fontSize = this.height + 4;
Font font;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(this.familyName, fontSize, FontStyle.Regular);
size = g.MeasureString(this.text, font);
} while (size.Width > this.width);
// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format);
// Draw the text.
hatchBrush = new HatchBrush(HatchStyle.Vertical, ColorTranslator.FromHtml("#000000"), ColorTranslator.FromHtml("#000000"));
g.FillPath(hatchBrush, path);
// Clean up.
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
// Set the image.
this.image = bitmap;
}
}
}
The Another Model Is
PageHeadingResult
2)---------------
using System.Web;
using System.Web.Mvc;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;
using System;
namespace WeddingWindow.Models
{
public class PageHeadingResult:ActionResult
{
public string _captchaText;
public PageHeadingResult(string captchaText)
{
_captchaText = captchaText;
}
public override void ExecuteResult(ControllerContext context)
{
try
{
PageHeading c = new PageHeading();
//Captcha c = new Captcha();
c.Text = _captchaText;
c.Width = 685;
c.Height = 50;
c.FamilyName = "FlemishScript BT";
HttpContextBase cb = context.HttpContext;
cb.Response.Clear();
cb.Response.ContentType = "image/Png";
c.Image.Save(cb.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
// Showing the Error A generic error occurred in GDI+
c.Dispose();
}
catch (Exception exp)
{
string msg = exp.Message;
}
}
}
}
3)-----------
Using The Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeddingWindow.Models;
using WeddingWindow.Helpers;
using System.Web.Security;
using System.IO;
using System.Collections;
using System.Web.UI.WebControls;
namespace WeddingWindow.Controllers
{
public class PreviewSiteController : Controller
{
//
// GET: /PreviewSite/
#region Variable declaration
LinqWeddingDataContext lnq = new LinqWeddingDataContext();
private static string UserName = null;
#endregion
#region Image Generation for Page Heading
public PageHeadingResult GetCaptcha()
{
var mpage = lnq.wtblManagePages.Single(u => u.UserId == User.Identity.Name && u.ActionName == "Welcome");
string captchaText = mpage.PageName;
HttpContext.Session.Add("captcha", captchaText);
return new PageHeadingResult(captchaText);
}
}
}
4)----------------
The Welcome.aspx Page
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/PreviewSiteMaster.Master" Inherits="System.Web.Mvc.ViewPage
<%--<%@ Register Src="~/Views/Shared/ImageControl.ascx" TagPrefix="Nimg" TagName="Image" %>--%>
<%-- Calling Getcaptcha Method to PreviewSite Controller --%>
<%--
<%=ViewData["PageName"]%>
--%><%--
<%--
<%--
<%: Model.Header %>
<%--
<%: Model.SubHeader %>
<%= ViewData["PageData"] %>
<%--
I am using all these things but the png image is not created
please anybody help me how to do it.
Thanks
Sivaraman DhamodaranPosted Sep 28, 2011, 4:18 AM
1) Yes as already other said, do the step by Step debugging and you'll narrows down to a statement that causing the trouble. I believe it is your save function
2) Use the string that has a message for you. As SAM said, display the message in messagebox or send back to the client (WebApp) or Log that into some trace file.
Sam HobbsPosted Sep 27, 2011, 3:16 PM
Also, you did not do everything that Mahesh said to do. You did not debug the program "line by line" (do you know how to do that?) because when you do, you will see the error.
Manoj TambePosted Sep 27, 2011, 1:39 PM
using System.Web.Mvc;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing;
using System;
namespace WeddingWindow.Models
{
public class PageHeadingResult:ActionResult
{
public string _captchaText;
public PageHeadingResult(string captchaText)
{
_captchaText = captchaText;
}
public override void ExecuteResult(ControllerContext context)
{
try
{
PageHeading c = new PageHeading();
//Captcha c = new Captcha();
c.Text = _captchaText;
c.Width = 685;
c.Height = 50;
c.FamilyName = "FlemishScript BT";
HttpContextBase cb = context.HttpContext;
//MemoryStream MemStream = new MemoryStream();
cb.Response.Clear();
cb.Response.ContentType = "image/Png";
c.Image.Save(cb.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png);
//c.Image.Save(cb.Response.OutputStream, ImageFormat.Gif);
c.Dispose();
}
catch (Exception exp)
{
string msg = exp.Message;
}
}
}
}
This is not showing error but
also not display image on the view page
i am using the asp.net mvc arch.
Mahesh ChandPosted Sep 27, 2011, 1:00 PM
Also, try to debug line by line and click on the object where error is occurred, expand it and get more details from there. Also, debug exp object and see what other details it has.
try
{
// Put your code here
}
catch (Exception exp)
{
string msg = exp.Message;
}