Hi All,
I've got a problem when uploading images using asp.net, shortly explain is...
Loop to get each picture in HttpPostedFile. Then calculate color for adding text to it
The error will occur on the second or some time third loop at "System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myimage);"
Any body had experienced through this before, please suggest, would be many thanks.
This is my codes:
private void saveFile(HttpPostedFile hpf, string strFileName)
{
System.Drawing.Image myimage = System.Drawing.Image.FromStream(hpf.InputStream, false, false);
int imageSize = hpf.ContentLength;
hpf = null;
colorProcessing cp = averageColor(cropImage(myimage));
addText(myimage,cp);
myimage.Save(Server.MapPath("~/PlantsPhoto/") + strFileName);
myimage.Dispose();
System.Threading.Thread.Sleep(imageSize / 10);
}
public void addText(System.Drawing.Image myimage, colorProcessing cp)
{
var w = myimage.Width;
var h = myimage.Height;
double gaugeSize = h >= w ? h / 100 : w / 100;
double fontSize = ((gaugeSize / 10) * (10 - (gaugeSize / 10))) + 4;
System.Drawing.StringFormat strFormat = new System.Drawing.StringFormat();
strFormat.Alignment = System.Drawing.StringAlignment.Near;
strFormat.LineAlignment = System.Drawing.StringAlignment.Near;
System.Drawing.Brush b= cp.colorTone!=0?System.Drawing.Brushes.White:System.Drawing.Brushes.Black;
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(myimage);
g.DrawString("xx©Copyright", new System.Drawing.Font("Tahoma", (int)fontSize), b,
new System.Drawing.RectangleF(0, 0, w / 2, h / 2), strFormat);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.Dispose();
myimage.Dispose();
strFormat.Dispose();
cp.Dispose();
b.Dispose();
}
private System.Drawing.Bitmap cropImage(System.Drawing.Image img)
{
System.Drawing.Rectangle cropArea = new System.Drawing.Rectangle(0,0,img.Width / 2, img.Height/3);
System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(img);
System.Drawing.Bitmap bmpCrop = bmpImage.Clone(cropArea,bmpImage.PixelFormat);
bmpImage.Dispose();
return bmpCrop;
}
public colorProcessing averageColor(System.Drawing.Bitmap img)
{
System.Drawing.Bitmap MyBitmap = img;
System.Drawing.Rectangle Rect = new System.Drawing.Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
System.Drawing.Imaging.BitmapData BmpData = MyBitmap.LockBits(Rect,
System.Drawing.Imaging.ImageLockMode.ReadOnly,
MyBitmap.PixelFormat);
int Size = BmpData.Stride * MyBitmap.Height;
byte[] RGBs = new byte[Size];
IntPtr Pointer = BmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(Pointer, RGBs, 0, Size);
MyBitmap.UnlockBits(BmpData);
int Sum = 0;
int whiteCount=0;
for (int Index = 0; Index < RGBs.Length; Index++)
{
Sum += RGBs[Index];
if (RGBs[Index]<50)
{
whiteCount++;
}
}
int Average = Sum / RGBs.Length;
colorProcessing CPInfo = new colorProcessing();
CPInfo.avgColor = Average;
CPInfo.colorTone = (RGBs.Length / 15) > whiteCount ? colorProcessing.tone.White : colorProcessing.tone.Black;
MyBitmap.Dispose();
BmpData = null;
RGBs = null;
return CPInfo;
}