开发者

Created image from text cannot be displayed?

I have converted text to Image(.png) using ASP.NET C# in Visual Studio 2010. But the image created cannot be displayed in the browser after submitting and it shows an message

The image "http://localhost:49670/WebSite1/Default.aspx" cannot be displayed开发者_运维技巧, because it contains errors.

During debugging there is no error or warning or anything like that. How can I resolve this?


Looking at the Url there is a chance that you are rendering image intermixed with HTML content... Using ASHX is better option to render images - check out http://aspalliance.com/1322_Displaying_Images_in_ASPNET_Using_HttpHandlers.all


Try this....

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", 20, System.Drawing.FontStyle.Bold, 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.FromArgb(102, 102, 102)), 0, 0);
           objGraphics.Flush();

           return (objBmpImage);
       }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜