- Copy file to `destnation/uploads` folder with the same filename.
- See if `package.tif` exists. If it does not, create this file with the .Save() function. [Works so far very great].
- However, if `package.tif' exists, try to append the uploaded file to `package.tif`. When I call .SaveAdd() I get an GDI exception with no further explained messages.
I suspect that the file I am reading from is locked. So I should call .Dispose() before calling .SaveAdd() but I do need access to other pages of the .tif in the next cycle of a for loop.
(To test all of this, I created a Windows console application. I have all the rights I should have)
I am attaching the fullcode:
static void myfunc()
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("myfunc() entered");
string DossiersPad = "C:\\Users\\Kucuk\\Documents\\visual studio 2010\\Projects\\TiffForWebsite\\TiffForWebsite\\bin\\Debug";
string inputfile = "test.png";
string ToPath = DossiersPad + "\\" + inputfile + "-Pakket.tif";
string FromPath = DossiersPad + "\\" + inputfile;
Console.WriteLine("ToPath contains: [{0}]", ToPath);
//File.Copy(inputfile, sDestDir + "\\" + sDestFileName);
string ext = System.IO.Path.GetExtension(inputfile);
Console.WriteLine("Extension: [{0}]", ext);
ImageCodecInfo info = null;
foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
{
if (ice.MimeType == "image/tiff")
{
info = ice;
}
}
Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
Bitmap pages = null;
Bitmap bm = null;
System.IO.FileInfo f = new System.IO.FileInfo(ToPath);
int i = 0;
Console.WriteLine("file information loaded");
if (f.Exists == true)
{
Console.WriteLine("file already exist");
//pages = (Bitmap)System.Drawing.Image.FromFile(ToPath + "_old");
using (Stream stream = new FileStream(ToPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
Console.WriteLine("stream loaded");
using (System.IO.StreamReader srtif = new System.IO.StreamReader(stream))
{
Console.WriteLine("streamreader loaded");
using (System.Drawing.Bitmap bmtif = new System.Drawing.Bitmap(srtif.BaseStream))
{
Console.WriteLine("bitmap loaded");
System.Drawing.Bitmap[] images = new System.Drawing.Bitmap[bmtif.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)];
i = images.Count();
Console.WriteLine("file contains {0} page(s)", i.ToString());
if (i > 1)
{
for (Int32 imageIndex = 0; imageIndex <= images.GetUpperBound(0); imageIndex++)
{
bmtif.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, imageIndex);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
if (imageIndex == 0)
{
pages = bmtif;
pages.Save("_lock.tif", info, ep);
Console.WriteLine("writing first page to _lock.tif");
}
else
{
pages.SaveAdd(bmtif, ep);
Console.WriteLine("appending page to _lock.tif - Pagenumber: [{0}]", imageIndex.ToString());
}
}
}
else
{
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages = bmtif;
pages.Save("_lock.tif", info, ep);
Console.WriteLine("saving to single paged file to _lock.tif");
//pages.SaveAdd(System.Drawing.Image.FromFile((file.InputStream as System.IO.FileStream).Name), ep);
}
Console.WriteLine("copied existing file to _lock.tif");
Console.WriteLine("trying to append file to _lock.tif");
if (ext == ".tif")
{
Console.WriteLine("file to be appended is a .tif file");
using (System.IO.StreamReader _tsrtif = new System.IO.StreamReader(ToPath))
{
using (System.Drawing.Bitmap _tbmtif = new System.Drawing.Bitmap(_tsrtif.BaseStream))
{
System.Drawing.Bitmap[] _timages = new System.Drawing.Bitmap[_tbmtif.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)];
if (_timages.Count() > 1)
{
Console.WriteLine("file to be appended has more than 1 page");
for (Int32 _timageIndex = 0; _timageIndex <= _timages.GetUpperBound(0); _timageIndex++)
{
_tbmtif.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, _timageIndex);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
//pages.SaveAdd(_tbmtif, ep);
using (Bitmap bmfh = (Bitmap)System.Drawing.Image.FromStream(new MemoryStream(File.ReadAllBytes(ToPath))))
{
bmfh.SaveAdd(_tbmtif, ep);
}
}
}
else
{
Console.WriteLine("file to be appended has more than 1 page");
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
bm = (Bitmap)System.Drawing.Image.FromFile((ToPath));
//pages.SaveAdd(bm, ep);
using (Bitmap bmfh = (Bitmap)System.Drawing.Image.FromStream(new MemoryStream(File.ReadAllBytes(ToPath + "_old"))))
{
bmfh.SaveAdd(bm, ep);
}
}
}
}
}
else
{
Console.WriteLine("file to be appended is a {0} file", ext);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
//bm = (Bitmap)System.Drawing.Image.FromStream(file.InputStream);
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("calling Image.FromFile({0})", ToPath);
bm = (Bitmap)Image.FromFile(FromPath);
Console.ForegroundColor = ConsoleColor.White;
pages.SaveAdd(bm, ep);
}
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
}
}
}
else
{
Console.WriteLine("ToPath does not exist yet. Copying inputfile to ToPath");
using (Bitmap bmfh = (Bitmap)System.Drawing.Image.FromStream(new MemoryStream(File.ReadAllBytes(FromPath))))
{
bmfh.Save(ToPath, info, ep);
Console.WriteLine("successfully copied");
}
}
Console.WriteLine("end of function, press any key to exit...");
}
Exception happends at the end of this partion of code:
apsync
Member
37 Points
19 Posts
Locking problem (once again)
Nov 11, 2011 08:55 AM|LINK
Hi,
I am trying to do the following:
- User uploads file.
- Copy file to `destnation/uploads` folder with the same filename.
- See if `package.tif` exists. If it does not, create this file with the .Save() function. [Works so far very great].
- However, if `package.tif' exists, try to append the uploaded file to `package.tif`. When I call .SaveAdd() I get an GDI exception with no further explained messages.
I suspect that the file I am reading from is locked. So I should call .Dispose() before calling .SaveAdd() but I do need access to other pages of the .tif in the next cycle of a for loop.
(To test all of this, I created a Windows console application. I have all the rights I should have)
I am attaching the fullcode:
static void myfunc() { Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("myfunc() entered"); string DossiersPad = "C:\\Users\\Kucuk\\Documents\\visual studio 2010\\Projects\\TiffForWebsite\\TiffForWebsite\\bin\\Debug"; string inputfile = "test.png"; string ToPath = DossiersPad + "\\" + inputfile + "-Pakket.tif"; string FromPath = DossiersPad + "\\" + inputfile; Console.WriteLine("ToPath contains: [{0}]", ToPath); //File.Copy(inputfile, sDestDir + "\\" + sDestFileName); string ext = System.IO.Path.GetExtension(inputfile); Console.WriteLine("Extension: [{0}]", ext); ImageCodecInfo info = null; foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) { if (ice.MimeType == "image/tiff") { info = ice; } } Encoder enc = Encoder.SaveFlag; EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame); Bitmap pages = null; Bitmap bm = null; System.IO.FileInfo f = new System.IO.FileInfo(ToPath); int i = 0; Console.WriteLine("file information loaded"); if (f.Exists == true) { Console.WriteLine("file already exist"); //pages = (Bitmap)System.Drawing.Image.FromFile(ToPath + "_old"); using (Stream stream = new FileStream(ToPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { Console.WriteLine("stream loaded"); using (System.IO.StreamReader srtif = new System.IO.StreamReader(stream)) { Console.WriteLine("streamreader loaded"); using (System.Drawing.Bitmap bmtif = new System.Drawing.Bitmap(srtif.BaseStream)) { Console.WriteLine("bitmap loaded"); System.Drawing.Bitmap[] images = new System.Drawing.Bitmap[bmtif.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)]; i = images.Count(); Console.WriteLine("file contains {0} page(s)", i.ToString()); if (i > 1) { for (Int32 imageIndex = 0; imageIndex <= images.GetUpperBound(0); imageIndex++) { bmtif.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, imageIndex); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); if (imageIndex == 0) { pages = bmtif; pages.Save("_lock.tif", info, ep); Console.WriteLine("writing first page to _lock.tif"); } else { pages.SaveAdd(bmtif, ep); Console.WriteLine("appending page to _lock.tif - Pagenumber: [{0}]", imageIndex.ToString()); } } } else { ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); pages = bmtif; pages.Save("_lock.tif", info, ep); Console.WriteLine("saving to single paged file to _lock.tif"); //pages.SaveAdd(System.Drawing.Image.FromFile((file.InputStream as System.IO.FileStream).Name), ep); } Console.WriteLine("copied existing file to _lock.tif"); Console.WriteLine("trying to append file to _lock.tif"); if (ext == ".tif") { Console.WriteLine("file to be appended is a .tif file"); using (System.IO.StreamReader _tsrtif = new System.IO.StreamReader(ToPath)) { using (System.Drawing.Bitmap _tbmtif = new System.Drawing.Bitmap(_tsrtif.BaseStream)) { System.Drawing.Bitmap[] _timages = new System.Drawing.Bitmap[_tbmtif.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)]; if (_timages.Count() > 1) { Console.WriteLine("file to be appended has more than 1 page"); for (Int32 _timageIndex = 0; _timageIndex <= _timages.GetUpperBound(0); _timageIndex++) { _tbmtif.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, _timageIndex); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); //pages.SaveAdd(_tbmtif, ep); using (Bitmap bmfh = (Bitmap)System.Drawing.Image.FromStream(new MemoryStream(File.ReadAllBytes(ToPath)))) { bmfh.SaveAdd(_tbmtif, ep); } } } else { Console.WriteLine("file to be appended has more than 1 page"); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); bm = (Bitmap)System.Drawing.Image.FromFile((ToPath)); //pages.SaveAdd(bm, ep); using (Bitmap bmfh = (Bitmap)System.Drawing.Image.FromStream(new MemoryStream(File.ReadAllBytes(ToPath + "_old")))) { bmfh.SaveAdd(bm, ep); } } } } } else { Console.WriteLine("file to be appended is a {0} file", ext); ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); //bm = (Bitmap)System.Drawing.Image.FromStream(file.InputStream); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("calling Image.FromFile({0})", ToPath); bm = (Bitmap)Image.FromFile(FromPath); Console.ForegroundColor = ConsoleColor.White; pages.SaveAdd(bm, ep); } ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush); pages.SaveAdd(ep); } } } } else { Console.WriteLine("ToPath does not exist yet. Copying inputfile to ToPath"); using (Bitmap bmfh = (Bitmap)System.Drawing.Image.FromStream(new MemoryStream(File.ReadAllBytes(FromPath)))) { bmfh.Save(ToPath, info, ep); Console.WriteLine("successfully copied"); } } Console.WriteLine("end of function, press any key to exit..."); }Console.WriteLine("calling Image.FromFile({0})", ToPath);bm = (Bitmap)Image.FromFile(FromPath);
Console.ForegroundColor = ConsoleColor.White;
pages.SaveAdd(bm, ep); // <-- here