Click here to Skip to main content
16,005,241 members
Home / Discussions / ASP.NET
   

ASP.NET

 
QuestionRe: Populate Treeview with xmlnodes Pin
Sonia Gupta2-Jan-08 23:17
Sonia Gupta2-Jan-08 23:17 
GeneralRe: Populate Treeview with xmlnodes Pin
N a v a n e e t h2-Jan-08 23:26
N a v a n e e t h2-Jan-08 23:26 
QuestionRe: Populate Treeview with xmlnodes Pin
Sonia Gupta2-Jan-08 23:50
Sonia Gupta2-Jan-08 23:50 
GeneralRe: Populate Treeview with xmlnodes Pin
N a v a n e e t h2-Jan-08 23:58
N a v a n e e t h2-Jan-08 23:58 
QuestionRe: Populate Treeview with xmlnodes Pin
Sonia Gupta3-Jan-08 0:14
Sonia Gupta3-Jan-08 0:14 
GeneralRe: Populate Treeview with xmlnodes Pin
Christian Graus2-Jan-08 22:21
protectorChristian Graus2-Jan-08 22:21 
GeneralRe: Populate Treeview with xmlnodes Pin
Michael Sync2-Jan-08 23:14
Michael Sync2-Jan-08 23:14 
GeneralCreate text image using handler Pin
Imran Khan Pathan2-Jan-08 20:05
Imran Khan Pathan2-Jan-08 20:05 
Hi friends.

I have coded to create text image using handler.But i can not get clarity of the image.

please visit these links.

BuildAsign[^] and Mysite[^]

My handler code here




public class getImage : IHttpHandler
{
MemoryStream PlotText(string text)
{

try
{
string fontFamily = GetArg("family", "Verdana");
float fontSize = float.Parse(GetArg("size", "12")); // assumed to be pixels
bool fontBold = bool.Parse(GetArg("bold", "false"));
bool fontItalic = bool.Parse(GetArg("italic", "false"));
bool fontUnderline = bool.Parse(GetArg("underline", "false"));
int iWidth = int.Parse(GetArg("width", "100"));
int iHeight = int.Parse(GetArg("height", "100"));
//int action = int.Parse(GetArg("action", "0"));
iHeight = int.Parse(fontSize.ToString()) + 10;
string isImage = GetArg("isImage", "true");
Color fontColor = GetColor(GetArg("color", "Black"));

//this is namespace for true virtual catch


if (text.Length > 100)
text = text.Substring(0, 100);
if (fontSize > 400)
fontSize = 400;

iWidth = text.Length * int.Parse(fontSize.ToString());
SizeF size = new SizeF(1, 1);
Font font = null;

try
{
Bitmap bitmap = new Bitmap(1, 1);

Graphics g = Graphics.FromImage(bitmap);

g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint =TextRenderingHint.AntiAlias;
g.SmoothingMode = SmoothingMode.HighQuality;


FontStyle fontStyle = FontStyle.Regular;
if (fontBold) fontStyle |= FontStyle.Bold;
if (fontItalic) fontStyle |= FontStyle.Italic;
if (fontUnderline) fontStyle |= FontStyle.Underline;
//fontSize = ((float)(iWidth / iHeight)) + iWidth;
font = new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.World);
// could get fancy here with StringFormat options
size = g.MeasureString(text, font);


int mW = int.Parse(fontSize.ToString()) / 2;

int w = mW / 2;

//if (isImage == "true")
// bitmap = new Bitmap((int)size.Width+w - mW, (int)size.Height - mW, PixelFormat.Format64bppPArgb);
//else
bitmap = new Bitmap((int)size.Width, (int)size.Height, PixelFormat.Format64bppPArgb);

g = Graphics.FromImage(bitmap);


SolidBrush br = new SolidBrush(Color.White);

g.FillRectangle(br, 0, 0, bitmap.Width, bitmap.Height);


br = new SolidBrush(fontColor);

//StringFormat sf = new StringFormat();
//sf.Alignment = StringAlignment.Center;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//g.TextRenderingHint = TextRenderingHint.AntiAlias;
//if (isImage == "true")
// g.DrawString(text, font, br, new PointF(-w + 5, -w + 2));
//else
g.DrawString(text, font, br, new PointF(0,0));




MemoryStream m = new MemoryStream();
bitmap.Save(m, ImageFormat.Gif);

return MakeTransparent(m);
}



finally
{
if (font != null) font.Dispose();
}

}
catch
{
return PlotError();
}
}

MemoryStream PlotError()
{

MemoryStream m = new MemoryStream();

using (Bitmap bitmap = new Bitmap(40, 15, PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
using (SolidBrush br = new SolidBrush(Color.White))
{
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.FillRectangle(br, 0, 0, bitmap.Width, bitmap.Height);
}
using (Pen p = new Pen(Color.Red))
{
g.DrawLine(p, 0, 0, bitmap.Width, bitmap.Height);
g.DrawLine(p, 0, bitmap.Height, bitmap.Width, 0);
}
bitmap.Save(m, ImageFormat.Gif);
}
}
return MakeTransparent(m);
}

void ok()
{
SetResponse(HttpStatusCode.OK, "Ok");
}

void error()
{
error(HttpStatusCode.BadRequest, "Bad Request");
}
void error(HttpStatusCode statusCode, string statusDescription)
{
SetResponse(statusCode, statusDescription);
_response.BinaryWrite(PlotError().ToArray());
_response.End();
}


#region ---- Process Request -----
HttpRequest _request;
HttpResponse _response;

public void ProcessRequest(HttpContext context)
{

_request = context.Request;
_response = context.Response;

if (!(("GET" == _request.RequestType) || ("HEAD" == _request.RequestType)))
error(HttpStatusCode.MethodNotAllowed, "Method Not Allowed");

string text = GetArg("text", "");
if (text.Length == 0)
error();

ok();

_response.BinaryWrite(PlotText(text).ToArray());

}
#endregion

public bool IsReusable
{
get { return false; }
}

// --------------------------------------------------------------------

string GetArg(string argName, string argDefault)
{
string arg = _request.QueryString[argName];
if ((null == arg) || (arg.Length == 0)) return argDefault;
return arg;
}

void SetResponse(HttpStatusCode statusCode, string statusDescription)
{
_response.ContentType = "image/gif";
_response.StatusCode = (int)statusCode;
_response.StatusDescription = statusDescription;
_response.Flush();
}

Array Redim(Array origArray, int newSize)
{
Type t = origArray.GetType().GetElementType();
Array newArray = Array.CreateInstance(t, newSize);
Array.Copy(origArray, 0, newArray, 0, Math.Min(origArray.Length, newSize));
return newArray;
}

Color GetColor(string color)
{
if (color.StartsWith("#"))
{
return Color.FromArgb(IntFromHexRgbPart(color, RgbPart.RgbPartRed),
IntFromHexRgbPart(color, RgbPart.RgbPathGreen),
IntFromHexRgbPart(color, RgbPart.RgbPartBlue)
);
}
return Color.FromName(color);
}

enum RgbPart { RgbPartRed, RgbPathGreen, RgbPartBlue };

int IntFromHexRgbPart(string hexRgb, RgbPart part)
{
if ((null == hexRgb) || (hexRgb.Length == 0) || (!(hexRgb.StartsWith("#"))))
return 0;
try
{
switch (part)
{
case RgbPart.RgbPartRed:
if (hexRgb.Length < 3) return 0;
return IntFromHex(hexRgb.Substring(1, 2));
case RgbPart.RgbPathGreen:
if (hexRgb.Length < 5) return 0;
return IntFromHex(hexRgb.Substring(3, 2));
case RgbPart.RgbPartBlue:
if (hexRgb.Length < 7) return 0;
return IntFromHex(hexRgb.Substring(5, 2));
default:
return 0;
}
}
catch { return 0; }
}

int IntFromHex(string hex)
{
return (int)byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}

MemoryStream MakeTransparent(MemoryStream origBitmapMemoryStream)
{

Color transparentColor = GetColor("White");
int transparentArgb = transparentColor.ToArgb();

using (Bitmap origBitmap = new Bitmap(origBitmapMemoryStream))
{
using (Bitmap newBitmap = new Bitmap(origBitmap.Width, origBitmap.Height, origBitmap.PixelFormat))
{

ColorPalette origPalette = origBitmap.Palette;
ColorPalette newPalette = newBitmap.Palette;

int index = 0;
int transparentIndex = -1;

foreach (Color origColor in origPalette.Entries)
{
newPalette.Entries[index] = Color.FromArgb(255, origColor);
if (origColor.ToArgb() == transparentArgb) transparentIndex = index;
index += 1;
}

if (-1 == transparentIndex)
{
return origBitmapMemoryStream;
}

newPalette.Entries[transparentIndex] = Color.FromArgb(0, transparentColor);
newBitmap.Palette = newPalette;

Rectangle rect = new Rectangle(0, 0, origBitmap.Width, origBitmap.Height);

BitmapData origBitmapData = origBitmap.LockBits(rect, ImageLockMode.ReadOnly, origBitmap.PixelFormat);
BitmapData newBitmapData = newBitmap.LockBits(rect, ImageLockMode.WriteOnly, newBitmap.PixelFormat);

for (int y = 0; y < origBitmap.Height; y++)
{
for (int x = 0; x < origBitmap.Width; x++)
{
byte origBitmapByte = Marshal.ReadByte(origBitmapData.Scan0, origBitmapData.Stride * y + x);
Marshal.WriteByte(newBitmapData.Scan0, newBitmapData.Stride * y + x, origBitmapByte);
}
}

newBitmap.UnlockBits(newBitmapData);
origBitmap.UnlockBits(origBitmapData);

MemoryStream m = new MemoryStream();

newBitmap.Save(m, ImageFormat.Gif);
return m;

}
}

}

}

please don't forget to vote on the post that helped you.

GeneralOWC webchart issue Pin
uglyeyes2-Jan-08 20:01
uglyeyes2-Jan-08 20:01 
Questionfaster execution in case of anchor tags , link buttons Pin
Sonia Gupta2-Jan-08 19:58
Sonia Gupta2-Jan-08 19:58 
GeneralRe: faster execution in case of anchor tags , link buttons Pin
N a v a n e e t h2-Jan-08 20:00
N a v a n e e t h2-Jan-08 20:00 
GeneralRe: faster execution in case of anchor tags , link buttons Pin
Sonia Gupta2-Jan-08 20:09
Sonia Gupta2-Jan-08 20:09 
GeneralRe: faster execution in case of anchor tags , link buttons Pin
Christian Graus2-Jan-08 22:10
protectorChristian Graus2-Jan-08 22:10 
GeneralCreating a directory or folder ` Pin
.NET- India 2-Jan-08 19:57
.NET- India 2-Jan-08 19:57 
GeneralRe: Creating a directory or folder ` Pin
N a v a n e e t h2-Jan-08 19:59
N a v a n e e t h2-Jan-08 19:59 
GeneralRe: Creating a directory or folder ` Pin
.NET- India 2-Jan-08 20:23
.NET- India 2-Jan-08 20:23 
GeneralRe: Creating a directory or folder ` Pin
N a v a n e e t h2-Jan-08 20:27
N a v a n e e t h2-Jan-08 20:27 
GeneralDropdown Textbox in asp.net Pin
anujose2-Jan-08 19:51
anujose2-Jan-08 19:51 
GeneralRe: Dropdown Textbox in asp.net Pin
N a v a n e e t h2-Jan-08 19:55
N a v a n e e t h2-Jan-08 19:55 
GeneralRe: Dropdown Textbox in asp.net Pin
anujose2-Jan-08 20:21
anujose2-Jan-08 20:21 
GeneralRe: Dropdown Textbox in asp.net Pin
Michael Sync2-Jan-08 21:09
Michael Sync2-Jan-08 21:09 
GeneralRe: Dropdown Textbox in asp.net Pin
Vasudevan Deepak Kumar2-Jan-08 19:58
Vasudevan Deepak Kumar2-Jan-08 19:58 
GeneralRe: Dropdown Textbox in asp.net Pin
rahul.net112-Jan-08 20:04
rahul.net112-Jan-08 20:04 
Generalproblems in using servlets and applets in asp Pin
chanzeb2-Jan-08 19:50
chanzeb2-Jan-08 19:50 
GeneralRe: problems in using servlets and applets in asp Pin
Vasudevan Deepak Kumar2-Jan-08 19:57
Vasudevan Deepak Kumar2-Jan-08 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.