G
Guest1993
Guest
I have a program that get user int input "1" and increment it based on the amount of files in a directory then stamp that int on each file( first is 1 and so on 1++). The foreach loop go in each directory gets its files, increment the input and call the stamp method until all files are done. In this process the order is important. However multitasking ( Parallel.ForEach) does't always guarantee order, in my understanding it returns which ever thread done first and maybe also damage the i++ functionality ( correct me if I'm wrong).
The question is how to apply multi threading in this case? i am thinking save the values of the foreach at the end, pass it to the stamping method and have the method stamp x amount of files at a time. I don't know if its possible or how to apply.
Here is my watermark method:
//text comes from the foreach already set.
public void waterMark(string text, string sourcePath, string destinationPathh)
{
using (Bitmap bitmap = new Bitmap(sourcePath))
{
int compressionTagIndex = Array.IndexOf(bitmap.PropertyIdList, 0x103);
PropertyItem compressionTag = bitmap.PropertyItems[compressionTagIndex];
byte[] com = compressionTag.Value;
Encoder encoder = Encoder.Compression;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(encoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/tiff");
Brush brush = new SolidBrush(Color.Black);
Font font = new Font("Arial", 50, FontStyle.Italic, GraphicsUnit.Pixel);
Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);
Graphics tempGraphics = Graphics.FromImage(tempBitmap);
SizeF textSize = tempGraphics.MeasureString(text, font);
tempBitmap = new Bitmap(bitmap.Width, bitmap.Height + (int)textSize.Height + 10);
tempBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(tempBitmap))
{
graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height + 100);
graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
Point position = new Point(bitmap.Width - ((int)textSize.Width + 200), bitmap.Height + 5);
graphics.DrawString((text), font, brush, position);
if (new[] { 2, 3, 4 }.Contains(com[0]))
{
tempBitmap.Save(destinationPathh, myImageCodecInfo, myEncoderParameters);
return;
}
tempBitmap.Save(destinationPathh, ImageFormat.Tiff);
}
}
}
I can provide the foreach loop if needed. Thank you in advance.
Continue reading...
The question is how to apply multi threading in this case? i am thinking save the values of the foreach at the end, pass it to the stamping method and have the method stamp x amount of files at a time. I don't know if its possible or how to apply.
Here is my watermark method:
//text comes from the foreach already set.
public void waterMark(string text, string sourcePath, string destinationPathh)
{
using (Bitmap bitmap = new Bitmap(sourcePath))
{
int compressionTagIndex = Array.IndexOf(bitmap.PropertyIdList, 0x103);
PropertyItem compressionTag = bitmap.PropertyItems[compressionTagIndex];
byte[] com = compressionTag.Value;
Encoder encoder = Encoder.Compression;
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(encoder, (long)EncoderValue.CompressionCCITT4);
myEncoderParameters.Param[0] = myEncoderParameter;
ImageCodecInfo myImageCodecInfo;
myImageCodecInfo = GetEncoderInfo("image/tiff");
Brush brush = new SolidBrush(Color.Black);
Font font = new Font("Arial", 50, FontStyle.Italic, GraphicsUnit.Pixel);
Bitmap tempBitmap = new Bitmap(bitmap.Width, bitmap.Height);
Graphics tempGraphics = Graphics.FromImage(tempBitmap);
SizeF textSize = tempGraphics.MeasureString(text, font);
tempBitmap = new Bitmap(bitmap.Width, bitmap.Height + (int)textSize.Height + 10);
tempBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);
using (Graphics graphics = Graphics.FromImage(tempBitmap))
{
graphics.FillRectangle(Brushes.White, 0, 0, bitmap.Width, bitmap.Height + 100);
graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
Point position = new Point(bitmap.Width - ((int)textSize.Width + 200), bitmap.Height + 5);
graphics.DrawString((text), font, brush, position);
if (new[] { 2, 3, 4 }.Contains(com[0]))
{
tempBitmap.Save(destinationPathh, myImageCodecInfo, myEncoderParameters);
return;
}
tempBitmap.Save(destinationPathh, ImageFormat.Tiff);
}
}
}
I can provide the foreach loop if needed. Thank you in advance.
Continue reading...