Can someone help me to understand what' wrong with the following code? I'm trying to draw an image from two images and save the drawn image to Response.OutputStream but I'm always getting the un-clear "A generic error occurred in GDI+" pointing to MyImage.Save(Response.OutputStream,
ImageFormat.Png);... thanks in advance.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
//using System.Collections.Generic;
//using System.Linq;
//using System.Web;
//using System.Web.UI;
//using System.Web.UI.WebControls;
public partial class ScoreImage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Image BackgroundImage = Image.FromFile(Server.MapPath("~/background.png"));
PixelFormat BackgroundImagePixelFormat = BackgroundImage.PixelFormat;
if (BackgroundImagePixelFormat.ToString().Contains("Indexed"))
{
BackgroundImagePixelFormat = PixelFormat.Format24bppRgb;
}
Image ScanlinesImage = Image.FromFile(Server.MapPath("~/scanlines.png"));
PixelFormat ScanlinesImagePixelFormat = ScanlinesImage.PixelFormat;
if (ScanlinesImagePixelFormat.ToString().Contains("Indexed"))
{
ScanlinesImagePixelFormat = PixelFormat.Format24bppRgb;
}
using (Bitmap MyImage = new Bitmap(ScanlinesImage.Width, ScanlinesImage.Height, ScanlinesImagePixelFormat))
{
using (Graphics MyCanvas = Graphics.FromImage(MyImage))
{
using (ImageAttributes MyImageAttributes = new ImageAttributes())
{
ColorMatrix MyColorMatrix = new ColorMatrix();
MyColorMatrix.Matrix33 = 0.33F;
MyImageAttributes.SetColorMatrix(MyColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
MyCanvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
MyCanvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
MyCanvas.SmoothingMode = SmoothingMode.AntiAlias;
MyCanvas.DrawImage(BackgroundImage, new Point(0, 0));
MyCanvas.DrawImage(
ScanlinesImage,
new Rectangle(0, 0, ScanlinesImage.Width, ScanlinesImage.Height),
0, 0, ScanlinesImage.Width, ScanlinesImage.Height,
GraphicsUnit.Pixel,
MyImageAttributes);
BackgroundImage.Dispose();
ScanlinesImage.Dispose();
}
}
//MyImage.Save(Server.MapPath("~/Result.png"), ScanlinesImage.RawFormat);
Response.ContentType = "image/png";
MyImage.Save(Response.OutputStream, ImageFormat.Png);
}
}
}
It isn't important where you stand in life, but in what direction you are moving.
Essam
Member
57 Points
24 Posts
A generic error occurred in GDI+
Dec 06, 2008 06:29 AM|LINK
Can someone help me to understand what' wrong with the following code? I'm trying to draw an image from two images and save the drawn image to Response.OutputStream but I'm always getting the un-clear "A generic error occurred in GDI+" pointing to MyImage.Save(Response.OutputStream, ImageFormat.Png);... thanks in advance.
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; //using System.Collections.Generic; //using System.Linq; //using System.Web; //using System.Web.UI; //using System.Web.UI.WebControls; public partial class ScoreImage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Image BackgroundImage = Image.FromFile(Server.MapPath("~/background.png")); PixelFormat BackgroundImagePixelFormat = BackgroundImage.PixelFormat; if (BackgroundImagePixelFormat.ToString().Contains("Indexed")) { BackgroundImagePixelFormat = PixelFormat.Format24bppRgb; } Image ScanlinesImage = Image.FromFile(Server.MapPath("~/scanlines.png")); PixelFormat ScanlinesImagePixelFormat = ScanlinesImage.PixelFormat; if (ScanlinesImagePixelFormat.ToString().Contains("Indexed")) { ScanlinesImagePixelFormat = PixelFormat.Format24bppRgb; } using (Bitmap MyImage = new Bitmap(ScanlinesImage.Width, ScanlinesImage.Height, ScanlinesImagePixelFormat)) { using (Graphics MyCanvas = Graphics.FromImage(MyImage)) { using (ImageAttributes MyImageAttributes = new ImageAttributes()) { ColorMatrix MyColorMatrix = new ColorMatrix(); MyColorMatrix.Matrix33 = 0.33F; MyImageAttributes.SetColorMatrix(MyColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); MyCanvas.InterpolationMode = InterpolationMode.HighQualityBicubic; MyCanvas.PixelOffsetMode = PixelOffsetMode.HighQuality; MyCanvas.SmoothingMode = SmoothingMode.AntiAlias; MyCanvas.DrawImage(BackgroundImage, new Point(0, 0)); MyCanvas.DrawImage( ScanlinesImage, new Rectangle(0, 0, ScanlinesImage.Width, ScanlinesImage.Height), 0, 0, ScanlinesImage.Width, ScanlinesImage.Height, GraphicsUnit.Pixel, MyImageAttributes); BackgroundImage.Dispose(); ScanlinesImage.Dispose(); } } //MyImage.Save(Server.MapPath("~/Result.png"), ScanlinesImage.RawFormat); Response.ContentType = "image/png"; MyImage.Save(Response.OutputStream, ImageFormat.Png); } } }Essam
Member
57 Points
24 Posts
Re: A generic error occurred in GDI+
Dec 06, 2008 07:07 AM|LINK
I found the answer here: http://aspalliance.com/319... I wouldn't imagine that such thing can be the problem!
Only if the exception message was a bit clearer *sigh*