This is the code which I run to generate a PDF
public string ScreenshotFullLength(string Url) {
UrlScreenshot Shot = new UrlScreenshot(Url, 975, 100);
int maxHeight = 1250;
// If you do skip the crop function you 'll get the
// full lenght of the page. We'll scale it to match
// a 400 pixel width
//int newHeight = (Shot.Bitmap.Width / 400) * Shot.Bitmap.Height;
//Shot.Resize(400, newHeight);
string Filename = LazyAssFilename();
string path = Server.MapPath("~") + "/tmp/" + Filename;
Shot.Bitmap.Save(path, ImageFormat.Png);
string pdfURL = "";
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
pdfURL = Server.MapPath("~") + "\\tmp\\Attest_" + EOFName + ".pdf";
PdfWriter.GetInstance(document, new FileStream(pdfURL, FileMode.Create));
// step 3: we open the document
document.Open();
// step 4: we add content
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(path);
if (jpg.Height > maxHeight) {
//we moeten er meer dan 1 maken en croppen
int loops = (int)(jpg.Height / maxHeight);
int rest = (int)(jpg.Height % maxHeight);
int i;
for (i = 0; i < loops; i++) {
Bitmap bmpImage = new Bitmap(path);
Bitmap bmpCrop = bmpImage.Clone(new System.Drawing.Rectangle(0, i * maxHeight, 975, maxHeight),
bmpImage.PixelFormat);
iTextSharp.text.Image crpd = iTextSharp.text.Image.GetInstance(bmpCrop, ImageFormat.Png);
crpd.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
crpd.ScalePercent(60);
document.Add(crpd);
}
//the rest
Bitmap bmpImage2 = new Bitmap(path);
Bitmap bmpCrop2 = bmpImage2.Clone(new System.Drawing.Rectangle(0, i * maxHeight, 975, rest),
bmpImage2.PixelFormat);
iTextSharp.text.Image crpdRest = iTextSharp.text.Image.GetInstance(bmpCrop2, ImageFormat.Png);
crpdRest.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
crpdRest.ScalePercent(60);
document.Add(crpdRest);
} else {
jpg.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
jpg.ScalePercent(60);
document.Add(jpg);
}
} catch (DocumentException de) {
Console.Error.WriteLine(de.Message);
} catch (IOException ioe) {
Console.Error.WriteLine(ioe.Message);
}
// step 5: we close the document
document.Close();
try {
//screenshots deleten
File.Delete(path);
} catch { }
return pdfURL;
}
This runs on a website, to make a PDF of a webpage.
However when multiple people access this code from the website, to generate their PDF's, I get the error: Object is currently in use elsewhere.
STACKTRACE: at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at ...
How would I fix this? The error gets thrown at Shot.Bitmap.Save(path, ImageFormat.Png);
I encountered the same problem, I have a worker thread pumping out GUI to its graphical interface, but it only throws this exception some of the time.
Wrapping it with an Invoke fixed it.
_parent.Invoke( new Action(() => eg.Graphics.DrawImage( _icon, rc )));
Just spotted this similar question. One of the answers suggests that GDI+ isn't threadsafe, in which case you'll need a lock around your Save() and maybe a few other methods.
Just to confirm, reading the information for the Image class, although it is ambiguous as to whether this means a single instance, or different instances of the same class.
Any public static (Shared in Visual Basic) members of this type are
thread safe. Any instance members are not guaranteed to be thread
safe.
Related
Windows 8.1 Pro, Visual Studio 2015 Update 3, C#, .NET Framework 4.5. Ghostscript.NET (latest), GhostScript 9.20.
I'm converting a PDF to a PDF. Hah. Well, I'm making an "editable" PDF "hard" PDF that can't be edited and is of lower quality. The process is I take the editable PDF, save it out as x-pages of PNG files, convert those PNG files to a multipage TIFF, and then convert the multipage TIFF to the PDF I need.
This worked just fine with Visual Studio 2012, one version earlier of GhostScript .NET and GS 9.10.
public static Tuple<string, List<string>> CreatePNGFromPDF(string inputFile, string outputfile)
{
Tuple<string, List<string>> t = null;
List<string> fileList = new List<string>();
string message = "Success";
string outputFileName = string.Empty;
int desired_x_dpi = 96;
int desired_y_dpi = 96;
try
{
using (GhostscriptViewer gsViewer = new GhostscriptViewer())
{
gsViewer.Open(inputFile);
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer(gsViewer))
{
for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
{
using (System.Drawing.Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber))
{
outputFileName = outputfile.Replace(".png", string.Empty) + "_page_" + pageNumber.ToString() + ".png";
img.Save(outputFileName, ImageFormat.Png);
if (!fileList.Contains(outputFileName))
{
fileList.Add(outputFileName);
}
}
}
}
}
}
catch (Exception ex)
{
message = ex.Message;
}
t = new Tuple<string, List<string>>(message, fileList);
return t;
}
This now fails on this line:
using (System.Drawing.Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber))
when processing the second page. The first page works okay.
I downloaded the source for GhostScript.NET, added it to my solution, debugged, etc., and spent a good long while trying to figure this out.
I then decided to separate out the functionality and make the bare minimum available for me to examine further in a simple Console application:
static void Main(string[] args)
{
int xDpi = 96;
int yDpi = 96;
string pdfFile = #"Inputfilenamehere.pdf";
GhostscriptVersionInfo gsVersionInfo = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL);
List<GhostscriptVersionInfo> gsVersionInfoList = GhostscriptVersionInfo.GetInstalledVersions(GhostscriptLicense.GPL | GhostscriptLicense.AFPL);
try
{
using (GhostscriptViewer gsViewer = new GhostscriptViewer())
{
gsViewer.Open(pdfFile);
using (GhostscriptRasterizer gsRasterizer = new GhostscriptRasterizer(gsViewer))
{
int pageCount = gsRasterizer.PageCount;
for (int i = 0; i < pageCount; i++)
{
Image img = gsRasterizer.GetPage(xDpi, yDpi, i + 1);
}
}
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Lo and behold, no problems. The difference is that I'm not putting declaration of my Image in the using statement.
I always try to be a good boy developer and use a using statement whenever the class implements IDisposable.
So, I removed the use of the using and I get the lower-quality PDF's that I've always desired. My life is good now.
using (GhostscriptViewer gsViewer = new GhostscriptViewer())
{
gsViewer.Open(inputFile);
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer(gsViewer))
{
for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
{
System.Drawing.Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
outputFileName = outputfile.Replace(".png", string.Empty) + "_page_" + pageNumber.ToString() + ".png";
img.Save(outputFileName, ImageFormat.Png);
if (!fileList.Contains(outputFileName))
{
fileList.Add(outputFileName);
}
}
}
}
Note that if I call img.Dispose() at the end of the for loop, I get the same error again!
My best guess is that my issue is not a GhostScript or GhostScript.NET issue. Am I being a bonehead for insisting on blindly using "using" statements if the class implements IDisposable? I've always understood that it's best practice to wrap anything that implements IDisposable with a using statement to forgo leaks, etc.
Hence, my question: Any ideas why I get the "Parameter is invalid" exception when I initialize the System.Drawing.Image class within the using statement but not when I don't? I'd love to understand this more.
Better yet, if anyone knows how I can get this functionality and also ensure I'm properly disposing my object, that would be the best.
I didn't find much about this particular topic when I searched for information. I did find one other StackOverflow post about someone using a graphic object in a using statement with the same error. I wonder if there is a relationship. I also note that I should be using Dispose(), but that appears to be causing the problem, and I need this to work.
FYI, for anyone interested, the actual error occurs here in GhostscriptInterprester.cs in the GhostScript.NET code:
Method: public void Run(string str)
str is "Page pdfshowpage_init pdfshowpage_finish"
// GSAPI: run the string
int rc_run = _gs.gsapi_run_string(_gs_instance, str, 0, out exit_code);
I found the root cause of my failure at least. My GhostscriptRasterizer object had a value of '0' set for the height points and width points.
var rasterizer = new GhostscriptRasterizer();
rasterizer.CustomSwitches.Add("-dDEVICEWIDTHPOINTS=" + widthPoints);
rasterizer.CustomSwitches.Add("-dDEVICEHEIGHTPOINTS=" + heightPoints);
Once I set both height and width to a valid non-zero value, the issue got fixed.
I want to compress a PNG image, to reduce its size but the quality should remain the same. I have tried to compress JPEG picture. Picture compressed about 90% and quality remain the same but when i compress a PNG image with it. No result, no compression. Same size.
Here is my code.
public const string _StatusLog = "StatusLog.csv";
static void Main(string[] args)
{
Console.WriteLine(" ### WELCOME ###");
Console.Write("\n\nPlease enter image folder path :");
string imagePath = Console.ReadLine();
Program p = new Program();
p.VaryQualityLevel(imagePath);
Console.ReadLine();
}
private void VaryQualityLevel(string pathOfImage)
{
try
{
//Console.Write("Target Directory Path :");
string targetDirectory = pathOfImage;//Console.ReadLine();
if (targetDirectory != null)
{
string[] allDirectoryInTargetDirectory = Directory.GetDirectories(targetDirectory);
//PRODUCT DIRECOTY OPEN
Console.Write("Total Folders found = " + allDirectoryInTargetDirectory.Count());
Console.Read();
if (allDirectoryInTargetDirectory.Any())
{
foreach (var directory in allDirectoryInTargetDirectory)
{
string[] subDirectory = Directory.GetDirectories(directory); // ATTRIBUTE DIRECTORY OPEN
if (subDirectory.Any())
{
foreach (var filesInSubDir in subDirectory)
{
string[] allFilesInSubDir = Directory.GetFiles(filesInSubDir);
//FILES IN SUB DIR OPEN
if (allFilesInSubDir.Any())
{
foreach (var imageFile in allFilesInSubDir)
{
try
{
Bitmap bmp1 = new Bitmap(imageFile);//pathOfImage);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
// Create an Encoder object based on the GUID
// for the Quality parameter category.
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
#region SAVING THE COMPRESS IMAGE FILE
EncoderParameters myEncoderParameters = new EncoderParameters(1);
EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
bmp1.Save(filesInSubDir + "\\" + "Zip" + GettingImageNameForOptimizedImage(imageFile), jpgEncoder, myEncoderParameters);//pathOfImage
Console.WriteLine(filesInSubDir + GettingImageNameForOptimizedImage(imageFile) + " CREATED");//pathOfImage
#endregion
#region DELETING THE ORIGNAL FILE
bmp1.Dispose();
System.IO.File.Delete(filesInSubDir + "\\" + GettingImageNameForOptimizedImage(imageFile));//pathOfImage
Console.WriteLine(imageFile.Replace("jpg", "png") + " DELETED");//pathOfImage
#endregion
//myEncoderParameter = new EncoderParameter(myEncoder, 100L);
//myEncoderParameters.Param[0] = myEncoderParameter;
//bmp1.Save("D:\\" + RemovingImageFormat[0] + "100L" + ".jpg", jpgEncoder, myEncoderParameters);
#region BACK RENAMING FILE TO ORIGNAL NAME
System.IO.File.Move(filesInSubDir + "\\" + "Zip" + GettingImageNameForOptimizedImage(imageFile), filesInSubDir + "\\" + GettingImageNameForOptimizedImage(imageFile));
#endregion
}
catch (Exception ex)
{
Console.Write("\n" + ex.Message + " Press enter to continue :");
Console.ReadLine();
Console.Write("\nWould you like to retry ? [Y/N] :");
string resp = Console.ReadLine();
if (resp == "Y" || resp == "y")
{
Console.WriteLine(" -------------------\n\n");
Main(null);
}
}
}
}
}
}
}
}
}
}
catch (Exception ex)
{
Console.Write(ex);
Console.Read();
}
Console.Write("Press any key to exit...");
Console.Read();
// Get a bitmap. ###################################################################
}
private ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
public string GettingImageNameForOptimizedImage(string pathOfImage)
{
try
{
string[] splitingPathOfImage = pathOfImage.Split('\\');
string[] RemovingImageFormat = splitingPathOfImage[splitingPathOfImage.Count() - 1].ToString().Split('.');
return RemovingImageFormat[0] + ".jpg";
}
catch (Exception)
{
return null;
}
return null;
}
public static void LoggingOperations(string ImageName, string Status, bool UpdateRequired)
{
try
{
if (!File.Exists(_StatusLog))
{
using (File.Create(_StatusLog)) { }
DirectorySecurity sec = Directory.GetAccessControl(_StatusLog);
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(_StatusLog, sec);
}
if (UpdateRequired == true)
{
string UpdateStatusText = File.ReadAllText(_StatusLog);
UpdateStatusText = UpdateStatusText.Replace(ImageName, ImageName + "," + Status);
File.WriteAllText(_StatusLog, UpdateStatusText);
UpdateStatusText = "";
}
else
{
File.AppendAllText(_StatusLog, Environment.NewLine);
File.AppendAllText(_StatusLog, Status);
}
}
catch (Exception)
{
}
}
For PNG compression i changed the following line.
Bitmap bmp1 = new Bitmap(imageFile);//pathOfImage);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Png);
Kindly some one help me out. If there is a new method, I welcome it. If this could be altered, it would a lot better.
PNG Images are 32 bits by default. You can convert them to 8 bits : resulting file will be about 5 times smaller than the original one. With most images, the loss of quality is almost invisible.
This is what online png compressors do.
You can do this yourself by using nQuant : http://nquant.codeplex.com/ (available on Nuget)
var quantizer = new WuQuantizer();
using(var quantized = quantizer.QuantizeImage(bmp1))
{
quantized.Save(targetPath, ImageFormat.Png);
}
Full explanation of the method is available on this blog post http://www.hurryupandwait.io/blog/convert-32-bit-pngs-to-high-quality-8-bit-pngs-with-c
I also suggest looking at ImageSharp which also works with .NET Core
using (var image = Image.Load(fileData)) // fileData could be file path or byte array etc.
{
var h = image.Size().Height / 2;
var w = image.Size().Width / 2;
var options = new ResizeOptions
{
Mode = ResizeMode.Stretch,
Size = new Size(w, h)
};
image.Mutate(_ => _.Resize(options));
using (var destStream = new MemoryStream())
{
var encoder = new JpegEncoder();
image.Save(destStream, encoder);
// Do something with output stream
}
}
The one major variable in PNG compression is the tradeoff between compression speed and output size. PNG compression can actually be quite slow because it involves searching a data buffer for matching patterns. You can speed up the compression by limiting how much of the buffer the encoder searches.
Your encoder should have a setting that allows you to specify how searching for matches it will do.
IF your input PNG image was not compressed with the encoder searching the entire buffer, you may get some improved compression by searching the full buffer in your application. However, you are unlikely to get a major improvement.
PNG should be losless given pixel size and color depth are consistent.
If looking for something to baseline output size against.
https://pnggauntlet.com/
I am trying to create a Windows app which uploads files to FTP. Essentially, it looks for .jpeg files in a given folder, it reads through the barcodes found in the .jpg files before uploading it into the FTP server, and entering the URL into the database for our records.
As there will be multiple files at any given time in the folder, I am essentially trying to read them in a loop, and process them accordingly. However, I get an OutOfMemoryException whenever the loop starts again. I am trying to figure out what I'm doing wrong here. I have appended my code below:
private void btnProcess_Click(object sender, RoutedEventArgs e)
{
podPath = Directory.GetFiles(DestPath, "*.jpg");
List<string> scans = new List<string>(podPath.Length);
List<string> badscans = new List<string>();
byte[] imageBytes;
string filename, result;
POD conpod = new POD();
OTPOD otpod = new OTPOD();
ConsignmentObj scanJob;
//Pickup OTScan;
//Consolidate ccv;
for (int i = 0; i < podPath.Count(); i++ )
{
filename = podPath[i].ToString();
using (Bitmap bm = (Bitmap)Bitmap.FromFile(filename))
{
var results = barcodeReader.Decode(bm);
result = results.ToString();
bm.Dispose();
}
if (result != null)
{
//if barcode can be read, we throw the value into the database to pull out relevant information
if (result.Contains(ConNotePrefix))
{
#region Consignments
scanJob = getCon(result.ToString());
final = ImageFolder + "\\" + result.ToString() + ".jpg";
using (System.Drawing.Image img = System.Drawing.Image.FromFile(filename))
{
MemoryStream ms = new MemoryStream();
try
{
img.Save(ms, ImageFormat.Jpeg);
imageBytes = ms.ToArray();
img.Dispose();
}
finally
{
ms.Flush();
ms.Close();
ms.Dispose();
}
}
lock (filename)
{
if (System.IO.File.Exists(filename))
{
File.Delete(filename);
}
}
using (var stream = File.Create(final)) { }
File.WriteAllBytes(final, imageBytes);
File.Delete(filename);
conpod.ConsignmentID = scanJob.ConsignmentID;
conpod.UserID = 1;
conpod.Location = ftpUrl + "//" + result.ToString() + ".jpg";
conpod.rowguid = Guid.NewGuid();
UploadFilesToFtp(ftpUrl, ftpUser, ftpPass, final, result.ToString() + ".jpg");
insertPOD(conpod);
scans.Add(result.ToString());
#endregion
}
}
else
{
badscans.Add(filename);
}
}
this.lbScans.ItemsSource = scans;
this.lbBadScans.ItemsSource = badscans;
}
The FTP method, UploadFilesToFtp(x, x, x, x, x, x) is not a problem here. All feedback will be much appreciated.
An OutOfMemoryException can also be thrown by the method FromFile of the Image class when
The file does not have a valid image format.
or
GDI+ does not support the pixel format of the file.
So i think there is a problem with one of your image files you are reading. One solution is to catch the OutOfMemoryException and adding the file to the badscans.
try{
using (Bitmap bm = (Bitmap)Bitmap.FromFile(filename)) {
var results = barcodeReader.Decode(bm);
result = results.ToString();
bm.Dispose();
}
}
catch(OutOfMemoryException) {
badscans.add(filename);
}
I have a program where Kinect gets a image and saves it, to a location the user specifies. I know the program finds the right folders since it creates more folders to save different kinds of images in, and those folders will be created. My current code (below) for saving the image works for other programs, so is there some parameter that is stopping it I am not aware of? Thanks in advance.
Saving the Image
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame == null)
{
return;
}
byte[] pixels = new byte[sensor.ColorStream.FramePixelDataLength];
//WriteableBitmap image = new WriteableBitmap(
// sensor.ColorStream.FrameWidth,
// sensor.ColorStream.FrameHeight, 96, 96,
// PixelFormats.Bgra32, null);
colorFrame.CopyPixelDataTo(pixels);
colorImage.WritePixels(new Int32Rect(0, 0, colorImage.PixelWidth,
colorImage.PixelHeight),
pixels, colorImage.PixelWidth * 4, 0);
//BitmapSource image = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
// 96, 96, PixelFormats.Bgr32, null,
// pixels, colorFrame.Width * 4);
//image.WritePixels(new Int32Rect(0, 0, image.PixelWidth, image.PixelHeight),
// pixels, image.PixelWidth * sizeof(int), 0);
//video.Source = image;
totalFrames++;
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(colorImage));
//path = System.IO.Path.Combine("C:/", "Kinected", "Images");
if (PersonDetected == true)
{
if (totalFrames % 10 == 0)
{
if (file_name != null && colorImage != null)
{
try
{
using (FileStream fs = new FileStream(colorPath +
#"\Kinected Image " + time + ".jpg", FileMode.Create))
{
encoder.Save(fs);
}
}
catch (IOException)
{
System.Windows.MessageBox.Show("Save Failed");
}
}
}
skeletonDeLbl.Content = "Skeleton Detected!";
}
if (PersonDetected == false) skeletonDeLbl.Content = "No Skeleton Detected.";
}
Determining the Path
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description =
"Select which folder you want Kinected to keep all of its information/images in.";
DialogResult result = dialog.ShowDialog();
colorPath = dialog.SelectedPath + #"\Color Images";
depthPath = dialog.SelectedPath + #"\Depth Images";
facePath = dialog.SelectedPath + #"\Face Data";
if (!Directory.Exists(colorPath))
Directory.CreateDirectory(colorPath);
if (!Directory.Exists(depthPath))
Directory.CreateDirectory(depthPath);
if (!Directory.Exists(facePath))
Directory.CreateDirectory(facePath);
System.Windows.MessageBox.Show(colorPath);
EDIT
Turns out file_name was just null, but now I am getting the error when it gets to the line using (FileStream fs = new FilesStream(file_name, FileMode.Create)) it says:
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll
Additional information: The given path's format is not supported.
Why is this happening? I am using the exact same code as Microsoft's demo, and it works fine there. Thanks.
You should use the following code to combine strings into a path
colorPath = System.IO.Path.Combine(dialog.SelectedPath, "Color Images");
The Combine method takes care of adding or removing backslashes where necessary.
And don't forget to use the debugger. You can set breakpoints and inspect the variables and do many more things.
The debugger is your best friend!
UPDATE
You are also using invalid characters in the filename. This method replaces invalid characters and applies also some other fixes to a filename
/// <summary>
/// Replaces invalid characters in a file name by " ". Apply only to the filename.ext
/// part, not to the path part.
/// </summary>
/// <param name="fileName">A file name (not containing the path part) possibly
/// containing invalid characters.</param>
/// <returns>A valid file name.</returns>
public static string GetValidFileName(string fileName)
{
string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
string s = Regex.Replace(fileName, "[" + invalidChars + "]", " ");
s = Regex.Replace(s, #"\s\s+", " "); // Replace multiple spaces by one space.
string fil = Path.GetFileNameWithoutExtension(s).Trim().Trim('.');
string ext = Path.GetExtension(s).Trim().Trim('.');
if (ext != "") {
fil += "." + ext;
}
fil = fil.Replace(" .", ".");
return fil == "." ? "" : fil;
}
how File.Delete() file temp.jpg with process A if locked by process B. how close handles file temp.jpg
IOExceoption:
The process cannot access the file Because it is being used by another process
protected void ButtonJcrop_Click(object sender, EventArgs e)
{
MembershipUser user = Membership.GetUser();
String tempPath = Server.MapPath("..") + #"\Users\" + user.ProviderUserKey.ToString() + #"\temp.gif";
System.Drawing.Image img = System.Drawing.Image.FromFile(tempPath);
Bitmap bmpCropped = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bmpCropped);
Rectangle rectDestination = new Rectangle(0, 0, bmpCropped.Width, bmpCropped.Height);
Rectangle rectCropArea = new Rectangle(Int32.Parse(hfX.Value), Int32.Parse(hfY.Value), Int32.Parse(hfWidth.Value), Int32.Parse(hfHeight.Value));
g.DrawImage(img, rectDestination, rectCropArea, GraphicsUnit.Pixel);
String mapPath = #"\Users\" + user.ProviderUserKey.ToString() + #"\" + user.ProviderUserKey.ToString() + ".gif";
bmpCropped.Save(Server.MapPath("..") + mapPath);
// bmpCropped.Save(Server.MapPath("..") + #"\Images\thumbs\CroppedImages\" + Session["WorkingImage"]);
imCropped.ImageUrl = Request.ApplicationPath + mapPath;
**File.Delete(tempPath);**
PlaceHolderImCropped.Visible = true;
}
Wait for process B to release the resource.
Pro-tip:
Process B locked the file for a reason. Stealing it is a bad idea in any situation I can think of that isn't pathological.
If you're in a pathological situation:
Get out of the pathological situation. You're only digging
yourself in deeper.
Kill process B.
Are there other techniques? Yes. They are by definition not safe, however, so don't do that.
The only way is for the locking process to pass control to the next process. Then you can catch the exception or the file will be locked until the locking process dies or passes control.
File tempPath is read by
System.Drawing.Image img
So before you delete that file, just use Dispose() method.
img.Dispose();
Bitmap bmpCropped = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bmpCropped);
Rectangle rectDestination = new Rectangle(0, 0, bmpCropped.Width, bmpCropped.Height);
Rectangle rectCropArea = new Rectangle(Int32.Parse(hfX.Value), Int32.Parse(hfY.Value), Int32.Parse(hfWidth.Value), Int32.Parse(hfHeight.Value));
using (System.Drawing.Image img = System.Drawing.Image.FromFile(tempPath)) g.DrawImage(img, rectDestination, rectCropArea, GraphicsUnit.Pixel);