A Graphics object cannot be created from an image that has an indexed pixel format.

  • Thread starter Thread starter HelpIsNice
  • Start date Start date
H

HelpIsNice

Guest
I have this issue as mentioned in the title. and here is what I tried to do to solve it: This is MVC 5 project.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(string text, int text1)
{
DirectoryInfo dir = new DirectoryInfo(@"C:\Users\name\Desktop\images");
FileInfo[] files = dir.GetFiles();
string value = text;
int value1 = text1;
//string file = Path.GetFileNameWithoutExtension(postedFile.FileName) + ".png";
foreach (FileInfo file in files)
{
// string newfile = Path.GetFileNameWithoutExtension(file.FileName) + ".png";
//if (postedFile != null)
//{
// Load document

using (Bitmap bitmap = new Bitmap(file.FullName))
{
Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);
using (Graphics graphics = Graphics.FromImage(tempBitmap))
{
graphics.DrawImage(bitmap, 0, 0);
// graphics.DrawLine();
Brush brush = new SolidBrush(Color.Black);
Font font = new Font("Arial", 10, FontStyle.Italic, GraphicsUnit.Pixel);
SizeF textSize = new SizeF();
textSize = graphics.MeasureString(value, font);
Point position = new Point(bitmap.Width - ((int)textSize.Width + 30), bitmap.Height - ((int)textSize.Height + 10));
graphics.DrawString((value + value1++), font, brush, position);
using (MemoryStream mStream = new MemoryStream())
{
mStream.Position = 0;
tempBitmap.Save(mStream, ImageFormat.Tiff);
string _path = Path.Combine(Server.MapPath("~/UploadedFolders"), file.Name);
tempBitmap.Save(_path);

}
}
}



}
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
ViewBag.Message = ("RunTime " + watch.ElapsedMilliseconds);
return View();
}


}


This solve the error However the output images are extremely small. My question is: how do I make the output stay as same size.

Note: if I watermark/draw on .png file this method works fine its .tif that gives me the issue. I don't know how to convert all files to .png then edit it and then convert it back to .tif maybe that is one approach thank you.

Continue reading...
 


Write your reply...
Back
Top