using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
namespace Techgrains.BusinessLayer.Admin
{
public class WriteToPng
{
public string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return String.Empty;
}
public ImageFormat GetImageFormat(String path)
{
switch (Path.GetExtension(path).ToLower())
{
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default: return null;
}
}
public byte[] GetByteData(string imagePath, string text, string Is16)
{
return AddTextToImage(imagePath, text, Is16);
}
public MemoryStream GetStream(string imagePath, string text, string Is16)
{
return new MemoryStream(GetByteData(imagePath, text, Is16));
//Image returnImage = Image.FromStream(ms);
//returnImage.Save(@"C:\test\testtest2.png");
}
public byte[] AddTextToImage(string imagePath, string text, string Is16)
{
byte[] imageBytes = null;
if (File.Exists(imagePath))
{
// This is the Name which will appear as a watermark on image.
string watermark = text;
Image image = Image.FromFile(imagePath);
Graphics graphic;
if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
{
graphic = Graphics.FromImage(image);
}
else
{
Bitmap indexedImage = new Bitmap(image);
graphic = Graphics.FromImage(indexedImage);
// Draw the contents of the original bitmap onto the new bitmap.
graphic.DrawImage(image, 0, 0, image.Width, image.Height);
image = indexedImage;
}
graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;
//This is the font for your watermark
Font myFont;
//Font myFont = new Font("Arial", 6);
SolidBrush brush = new SolidBrush(Color.Black);
//This gets the size of the graphic
SizeF textSize; //= graphic.MeasureString(watermark, myFont);
// Code for writing text on the image and showing its postion on images.
PointF pointF;
//To check weather image is in 16x16 format or not.
if (Is16 == "Yes")
{
myFont = new Font("Arial", 4);
textSize = graphic.MeasureString(watermark, myFont);
pointF = new PointF(3, 16);
}
else
{
myFont = new Font("Arial", 6);
textSize = graphic.MeasureString(watermark, myFont);
pointF = new PointF(4, 33);
}
graphic.DrawString(watermark, myFont, brush, pointF);
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, GetImageFormat(imagePath));
imageBytes = memoryStream.ToArray();
}
}
return imageBytes;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
namespace Techgrains.BusinessLayer.Admin
{
public class WriteToPng
{
public string GetContentType(String path)
{
switch (Path.GetExtension(path))
{
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: break;
}
return String.Empty;
}
public ImageFormat GetImageFormat(String path)
{
switch (Path.GetExtension(path).ToLower())
{
case ".bmp": return ImageFormat.Bmp;
case ".gif": return ImageFormat.Gif;
case ".jpg": return ImageFormat.Jpeg;
case ".png": return ImageFormat.Png;
default: return null;
}
}
public byte[] GetByteData(string imagePath, string text, string Is16)
{
return AddTextToImage(imagePath, text, Is16);
}
public MemoryStream GetStream(string imagePath, string text, string Is16)
{
return new MemoryStream(GetByteData(imagePath, text, Is16));
//Image returnImage = Image.FromStream(ms);
//returnImage.Save(@"C:\test\testtest2.png");
}
public byte[] AddTextToImage(string imagePath, string text, string Is16)
{
byte[] imageBytes = null;
if (File.Exists(imagePath))
{
// This is the Name which will appear as a watermark on image.
string watermark = text;
Image image = Image.FromFile(imagePath);
Graphics graphic;
if (image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
{
graphic = Graphics.FromImage(image);
}
else
{
Bitmap indexedImage = new Bitmap(image);
graphic = Graphics.FromImage(indexedImage);
// Draw the contents of the original bitmap onto the new bitmap.
graphic.DrawImage(image, 0, 0, image.Width, image.Height);
image = indexedImage;
}
graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;
//This is the font for your watermark
Font myFont;
//Font myFont = new Font("Arial", 6);
SolidBrush brush = new SolidBrush(Color.Black);
//This gets the size of the graphic
SizeF textSize; //= graphic.MeasureString(watermark, myFont);
// Code for writing text on the image and showing its postion on images.
PointF pointF;
//To check weather image is in 16x16 format or not.
if (Is16 == "Yes")
{
myFont = new Font("Arial", 4);
textSize = graphic.MeasureString(watermark, myFont);
pointF = new PointF(3, 16);
}
else
{
myFont = new Font("Arial", 6);
textSize = graphic.MeasureString(watermark, myFont);
pointF = new PointF(4, 33);
}
graphic.DrawString(watermark, myFont, brush, pointF);
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, GetImageFormat(imagePath));
imageBytes = memoryStream.ToArray();
}
}
return imageBytes;
}
}
}
No comments:
Post a Comment