Witam,
mam taki problem do DataGrid są ładowane dane z bazy danych i zrobione jest pagowanie. W jednej kolumnie są wyświetlane obrazki, które są wycinane i zapisywane do katalogu temp z większego obrazka. Chciałbym aby po przejściu na kolejną stronę zostały usunięte obrazki z poprzedniej strony, jednak przy próbie usunięcia mam brak dostępu do plików.
void PrintDt()
{
string sFolder = "";
string sFile = "";
string[] sFiles=new String[_dt.Rows.Count];
int iCounter = 0;
Configuration oConfig = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None );
foreach (DataRow row in _dt.Rows)
{
string pltrct = row["pltrct"].ToString();
string[] coords = pltrct.Split( ',' );
int x1 = int.Parse( coords[0] );
int y1 = int.Parse( coords[1] );
int x2 = int.Parse( coords[2] );
int y2 = int.Parse( coords[3] );
DateTime dtm = (DateTime)row["tm"];
sFolder = oConfig.AppSettings.Settings["imgpath"].Value + "\\" + row["cam"].ToString().Trim() + "\\" + dtm.ToString( "yyyyMMdd" );
sFile = row["filenum"].ToString().Trim() + "r.jpg";
sFiles[iCounter] = sFolder + "\\" + sFile;
if (sFile.Length > 5)
CutImage( sFolder, sFile, x1, y1, x2, y2 );
iCounter++;
}
}
/////////////////////////////////////////////////////////////////////
// cut image
private void CutImage( string sFolder, string sFile, int x1, int y1, int x2, int y2 )
{
int width = 0;
int height = 0;
string filePath = Environment.GetEnvironmentVariable( "temp" ) + "\\images"; //"C:\\Temp";
string[] files;
bool exists = System.IO.Directory.Exists( filePath );
if (!exists)
System.IO.Directory.CreateDirectory( filePath );
if( x1 != null & y1 != null & x2 != null & y2 != null )
{
width = x2 - x1;
height = y2 - y1; }
try{
files = System.IO.Directory.GetFiles( sFolder, sFile );
foreach( string path in files )
{
Bitmap src = new Bitmap(path);
string[] fileNames = path.Split('\\');
string fileName = fileNames[fileNames.Length - 1];
Bitmap CroppedImage = src.Clone( new System.Drawing.Rectangle( x1, y1, width, height ), src.PixelFormat );
try{
if (!File.Exists( filePath +"\\"+ fileName ))
CroppedImage.Save( filePath + "\\" + fileName );
}
catch (Exception ioex)
{
Console.WriteLine( ioex );
}
src.Dispose();
CroppedImage.Dispose();
}
}
catch (IOException ioex)
{
Console.WriteLine( ioex );
}
}
/////////////////////////////////////////////////////////////////////
// update _dt
void UpdateDT()
{
string sPath = Environment.GetEnvironmentVariable( "temp" ) + "\\images";
string[] files = Directory.GetFiles( sPath );
for (int i = 0; i < files.Length; i++)
{
files[i] = System.IO.Path.GetFileName( files[i] );
}
foreach (DataRow dr in _dt.Rows)
{
dr["image"] = null;
for (int i = 0; i < files.Length; i++)
{
if (dr["filenum"].ToString().Trim() == files[i].Remove( files[i].Length - 5 ))
{
dr["image"] = sPath + "\\" + files[i];
}
}
}
}
public static void ClearFolder( string FolderName )
{
try
{
Array.ForEach( Directory.GetFiles( FolderName ), File.Delete );
}
catch (IOException ioex){
//Console.WriteLine( ioex );
}
}
Jakieś pomysły,
z góry dziękuję i pozdrawiam
PiK