Thursday, February 18, 2010

Create Blank Image With Text On that

//*****************************************//

// Create Blank Image With Text On that

//*****************************************//

Image imgThumb = new Bitmap(176, 134, PixelFormat.Format24bppRgb);

MemoryStream memeorystreamObj = new MemoryStream();

System.Drawing.Bitmap bitmapObj = new System.Drawing.Bitmap(imgThumb);

Graphics objGraphics = Graphics.FromImage(bitmapObj);

Font objFont1 = new Font("Arial", 10, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Pixel);

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

objGraphics = Graphics.FromImage(bitmapObj);

// Set Background color

objGraphics.Clear(Color.White);

objGraphics.SmoothingMode = SmoothingMode.AntiAlias;

objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

objGraphics.DrawString("Sample Image for", objFont1, new SolidBrush(Color.FromArgb(000, 122, 102)), 0, 25);

objGraphics.DrawString(ThemeName, objFont, new SolidBrush(Color.FromArgb(102, 102, 000)), 0, 40);

objGraphics.Flush();

bitmapObj.Save(memeorystreamObj, ImageFormat.Jpeg);

byte[] imageByteArray = memeorystreamObj.ToArray();

ImageFormat imageFormat = ImageFormat. Jpeg;

string path = "c:\\Folder12\image123.jpg";

FileInfo f2 = new FileInfo(path);

if (!Directory.Exists(f2.Directory.ToString()))

Directory.CreateDirectory(f2.Directory.ToString());

try

{

ImageConverter converter = new ImageConverter();

Image img = null;

Byte[] imageByteArray = null;

img = (Image)converter.ConvertFrom(Imagestream);

MemoryStream stream = new MemoryStream();

img.Save(stream, imageFormat);

img = Image.FromStream(stream);

imageByteArray = (byte[])converter.ConvertTo(img, typeof(byte[]));

img.Dispose();

using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))

{

//saving the file as the image

fs.Write(imageByteArray, 0, imageByteArray.Length);

fs.Close();

fs.Dispose();

}

_sucess = true;

}

catch (Exception ex)

{

_sucess = false;

}

No comments: