How to take screenshot of desire windows - c#

I want to capture only some part of desktop screen. for example I've opened 4,5 different window explorer, browsers, some MS office files and other stuff. and I want to take screenshot which only include some of them(windows). Not All windows.
string appPath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory) + #"\screenshots\";
string imgName = name + DateTime.Now.Ticks + #".JPEG";
Bitmap memoryImage = null;
Graphics memoryGraphics = null;
int width = Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenWidth);
int height = Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenHeight);
System.Drawing.Size s = new System.Drawing.Size(width, height);
string str = "";
try
{
str = appPath + imgName;
}
catch (Exception er)
{
MessageBox.Show(er.Message.ToString());
}
using (memoryImage = new Bitmap(width, height))
{
using (memoryGraphics = Graphics.FromImage(memoryImage))
{
memoryGraphics.CopyFromScreen(0, 0, 0, 0, s);
memoryImage.Save(str);
}
}
memoryImage.Dispose();
memoryGraphics.Dispose();
I'm using above code, which simple give me screenshot of my desktop.

Related

Deleting file throws file in use exception

I have a file upload control that allows users to upload an image file. Rather then forcing them to use a specific size image, I am trying to resize it, save it the resized version to a folder and then delete the original.
When I try to delete the file, I keep getting "file in use" exception. I have searched and tried all suggestions to no avail.
This is the latest try, at which point I decided to seek help. I pass the source path (where original file is in, a destination path where resized file is to end up in, file name and width to which it should be resized, keeping aspect ratio):
public static void CreateResizedCopy(String sourcePath, String destinationPath, String filename, int width)
{
if (!File.Exists(destinationPath + filename))
{
System.Drawing.Image image = System.Drawing.Image.FromFile(sourcePath + filename);
float AspectRatio = (float)image.Size.Width / (float)image.Size.Height;
int newHeight = Convert.ToInt32(width / AspectRatio);
Bitmap thumbnailBitmap = new Bitmap(width, newHeight);
Graphics thumbnailGraph = Graphics.FromImage(thumbnailBitmap);
thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, width, newHeight);
thumbnailGraph.DrawImage(image, imageRectangle);
thumbnailBitmap.Save(destinationPath + filename, ImageFormat.Jpeg);
thumbnailGraph.Dispose();
thumbnailBitmap.Dispose();
image.Dispose();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
if (!FileInUse(sourcePath + filename))
{
File.Delete(sourcePath + filename);
}
else
{
// Error here: file is in use
using (FileStream fs = new FileStream(sourcePath + filename, FileMode.OpenOrCreate))
fs.Dispose();
}
}
}
public static bool FileInUse(string path)
{
try
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
return false;
}
}
catch (IOException ex)
{
return true;
}
}
UPDATE
This is the AJAX AsynFileUpload's upload completed event handler. It saves the file to a "Resize" folder. If image needs to be resized, it calls the method to resize it and then moves it to its final resting place. If not, no resizing is done, it just moves the file. It then "attempts" to delete the file in resize folder and it chokes!
protected void fuNewsImage_UploadedComplete_Resize(object sender, AsyncFileUploadEventArgs e)
{
if (fuNewsImage.HasFile)
{
HttpPostedFile file = fuNewsImage.PostedFile;
string sFileName = fuNewsImage.FileName;
string sFileSize = e.FileSize;
string sSaveFolder = ImageFilePath.TEMP_FOLDER_EA_TEST + "\\";
string sResizeFolder = ImageFilePath.TEMP_FOLDER_EA_TEST + "\\Resized\\";
fuNewsImage.SaveAs(Server.MapPath(sResizeFolder + sFileName));
System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath(sResizeFolder) + sFileName);
int iImgWidth = img.Width;
int iImgHeight = img.Height;
if (iImgWidth > ImageInfo.MAX_WIDTH)
{
// sSaveFolder is the efinal resting place of the uploaded image
// If image width is more than max allowed, resize it, save it in sSaveFolder
Utils.CreateResizedCopy(Server.MapPath(sResizeFolder), Server.MapPath(sSaveFolder), sFileName, 600);
System.Threading.Thread.Sleep(1000);
File.Delete(Server.MapPath(sSaveFolder + sFileName));
}
else // just move it t sSaveFolder
{
File.Move(Server.MapPath(sResizeFolder) + sFileName, Server.MapPath(sSaveFolder) + sFileName);
}
// --- Chokes here ---
Array.ForEach(Directory.GetFiles(Server.MapPath(sResizeFolder)), File.Delete);
}
}

Image.Save Parameter not valid

I want to watermark files in a folder with another image. However, am getting the error
'Parameter Not Valid'
when invoking the code
img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
I have the following code;
public static string WatermarkImagesInFolder(string url)
{
if (url == null)
throw new Exception("URL must be provided");
string path = HttpContext.Current.Server.MapPath(url);
if (!Directory.Exists(path))
throw new DirectoryNotFoundException();
Directory.CreateDirectory(String.Format(#"{0}\watermarked", path));
List<string> urls = GetJpgFilesFromFolder(path);
foreach (string imageUrl in urls)
{
Image img = WatermarkImage(imageUrl);
string filename = Path.GetFileName(imageUrl);
string filepath = String.Format(#"{0}\watermarked\{1}", path, filename);
img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
return "complete";
}
and
public static Image WatermarkImage(string filename)
{
using (Image image = Image.FromFile(filename))
using (Image watermarkImage = Image.FromFile(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["WatermarkImageUrl"])))
using (Graphics imageGraphics = Graphics.FromImage(image))
using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
{
int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
watermarkBrush.TranslateTransform(x, y);
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
return image;
}
}
as an example of input for img.save;
img.save("C:\\IMAGES\\wateremarked\\IMAGE (1).jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
any ideas on what could be causing the error here please?
In your WatermarkImage you have the image object in a using statement. As soon as that goes out of scope, Dispose is invoked on the image. You would need to return a copy of the image - or not use the using statement & ensure you dispose properly later.
See : Exception: Parameter is not valid (on passing new image to pictureBox)
public static string WatermarkImagesInFolder(string url)
{
if (url == null)
throw new Exception("URL must be provided");
string path = HttpContext.Current.Server.MapPath(url);
if (!Directory.Exists(path))
throw new DirectoryNotFoundException();
Directory.CreateDirectory(String.Format(#"{0}\watermarked", path));
List<string> urls = GetJpgFilesFromFolder(path);
foreach (string imageUrl in urls)
{
using(Image img = WatermarkImage(imageUrl))
{
string filename = Path.GetFileName(imageUrl);
string filepath = String.Format(#"{0}\watermarked\{1}", path, filename);
img.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
return "complete";
}
public static Image WatermarkImage(string filename)
{
Image image = Image.FromFile(filename);
using (Image watermarkImage = Image.FromFile(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["WatermarkImageUrl"])))
using (Graphics imageGraphics = Graphics.FromImage(image))
using (TextureBrush watermarkBrush = new TextureBrush(watermarkImage))
{
int x = (image.Width / 2 - watermarkImage.Width / 2);
int y = (image.Height / 2 - watermarkImage.Height / 2);
watermarkBrush.TranslateTransform(x, y);
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(x, y), new Size(watermarkImage.Width + 1, watermarkImage.Height)));
return image;
}
}
Here is my alternative solution, which gives the ability to use picture stream, but not it's location as it's currently browsed from Kendo Control as in my example and it still have not physical location yet.
public static Image ApplyWatermark(HttpPostedFileBase img, string appDataPath)
{
Image resultImage = null;
using (Image image = Image.FromStream(img.InputStream))
{
using (Image watermarkImage = Image.FromFile(appDataPath + "\\Watermark\\sample-watermark.png"))
{
using (Graphics imageGraphics = Graphics.FromImage(image))
{
using (Brush watermarkBrush = new TextureBrush(watermarkImage))
{
imageGraphics.FillRectangle(watermarkBrush, new Rectangle(new Point(0, 0), image.Size));
resultImage = (Image)image.Clone();
}
}
}
}
return resultImage;
}
private void SaveImagesOnDisk(IEnumerable<HttpPostedFileBase> images, int rentalPropertyId)
{
if (images != null)
{
foreach (var file in images)
{
// Some browsers send file names with full path. This needs to be stripped.
var fileName = Path.GetFileName(file.FileName);
var appDataPath = this.Server.MapPath("~/App_Data/");
var newPath = Path.Combine(appDataPath, rentalPropertyId.ToString());
if (!Directory.Exists(newPath))
{
DirectoryInfo di = Directory.CreateDirectory(newPath);
}
var physicalPath = Path.Combine(newPath, fileName);
System.Drawing.Image markedImage = Helper.ApplyWatermark(file, appDataPath);
markedImage.Save(physicalPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
//return this.Json(new { status = "OK" }, "text/plain");
}

I'm try to implement UPSP Label Generating

I am tryin to implement UPSP label generating but i am getting this error.
**API Authorization failure. DelivConfirmCertifyV3.0Request is not a valid API name for this protocol.**
if i test on browser, it's working fine
http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=%3CAddressValidateRequest%20USERID=%22Testuserid%22%3E%3CAddress%20ID=%221%22%3E%3CAddress1%3E%3C/Address1%3E%3CAddress2%3E10051+Orr+%26amp%3b+Day+Rd%3C/Address2%3E%3CCity%3Esanta+fe+springs%3C/City%3E%3CState%3Eca%3C/State%3E%3CZip5%3E90670%3C/Zip5%3E%3CZip4%3E%3C/Zip4%3E%3C/Address%3E%3C/AddressValidateRequest%3E
Anyone know about this problem...
Code Is below:
public Package GetDeliveryConfirmationLabel(Package package)
{
string labeldate = package.ShipDate.ToShortDateString();
if (package.ShipDate.ToShortDateString() == DateTime.Now.ToShortDateString())
labeldate = "";
string url = "?API=DelivConfirmCertifyV3.0Request&XML=<DelivConfirmCertifyV3.0Request.0Request USERID=\"{0}\"><Option>{1}</Option><ImageParameters></ImageParameters><FromName>{2}</FromName><FromFirm>{3}</FromFirm><FromAddress1>{4}</FromAddress1><FromAddress2>{5}</FromAddress2><FromCity>{6}</FromCity><FromState>{7}</FromState><FromZip5>{8}</FromZip5><FromZip4>{9}</FromZip4><ToName>{10}</ToName><ToFirm>{11}</ToFirm><ToAddress1>{12}</ToAddress1><ToAddress2>{13}</ToAddress2><ToCity>{14}</ToCity><ToState>{15}</ToState><ToZip5>{16}</ToZip5><ToZip4>{17}</ToZip4><WeightInOunces>{18}</WeightInOunces><ServiceType>{19}</ServiceType><POZipCode>{20}</POZipCode><ImageType>{21}</ImageType><LabelDate>{22}</LabelDate><CustomerRefNo>{23}</CustomerRefNo><AddressServiceRequested>{24}</AddressServiceRequested><SenderName>{25}</SenderName><SenderEMail>{26}</SenderEMail><RecipientName>{27}</RecipientName><RecipientEMail>{28}</RecipientEMail></DelivConfirmCertifyV3.0Request.0Request>";
url = GetURL() + url;
//url = String.Format(url,this._userid, (int)package.LabelType, package.FromAddress.Contact, package.FromAddress.FirmName, package.FromAddress.Address1, package.FromAddress.Address2, package.FromAddress.City, package.FromAddress.State, package.FromAddress.Zip, package.FromAddress.ZipPlus4, package.ToAddress.Contact, package.ToAddress.FirmName, package.ToAddress.Address1, package.ToAddress.Address2, package.ToAddress.City, package.ToAddress.State, package.ToAddress.Zip, package.ToAddress.ZipPlus4, package.WeightInOunces.ToString(), package.ServiceType.ToString().Replace("_", " "), package.OriginZipcode, package.LabelImageType.ToString(), labeldate, package.ReferenceNumber, package.AddressServiceRequested.ToString(), package.FromAddress.Contact, package.FromAddress.ContactEmail, package.ToAddress.Contact, package.ToAddress.ContactEmail);
url = String.Format(url, this._userid, (int)package.LabelType, package.FromAddress.Contact, package.FromAddress.FirmName, package.FromAddress.Address1, package.FromAddress.Address2, package.FromAddress.City, package.FromAddress.State, package.FromAddress.Zip, package.FromAddress.ZipPlus4, package.ToAddress.Contact, package.ToAddress.FirmName, package.ToAddress.Address1, package.ToAddress.Address2, package.ToAddress.City, package.ToAddress.State, package.ToAddress.Zip, package.ToAddress.ZipPlus4, package.WeightInOunces.ToString(), package.ServiceType.ToString().Replace("_", " "), package.OriginZipcode, package.LabelImageType.ToString(), labeldate, package.ReferenceNumber, package.AddressServiceRequested.ToString(), "", "", "", "");
string xml = web.DownloadString(url);
if (xml.Contains("<Error>"))
{
int idx1 = xml.IndexOf("<Description>") + 13;
int idx2 = xml.IndexOf("</Description>");
int l = xml.Length;
string errDesc = xml.Substring(idx1, idx2 - idx1);
throw new USPSManagerException(errDesc);
}
int i1 = xml.IndexOf("<DeliveryConfirmationLabel>") + 27;
int i2 = xml.IndexOf("</DeliveryConfirmationLabel>");
package.ShippingLabel = StringToUTF8ByteArray(xml.Substring(i1, i2 - i1));
return package;
}
I get the error on
int i1 = xml.IndexOf("<DeliveryConfirmationLabel>") + 27;
The solution to your problem is the same on this stackoverflow question- How to remove 'SAMPLE DO NOT MAIL' from USPS shipping API image
And you can find the steps to get this done from my answer on that question or by directly following this link- https://stackoverflow.com/a/27936025/3748701
USPS returns the Label in Base64 string you'd be required to convert that into an image. In my application i was required to provide the image as downloadable file, so I have returned the file result from the controller-action. Below is the action which gets the Base64 string label from DB, converts it into image and provides as downloadable file-
public ActionResult GetReturnShippingLabel(int orderId, bool showFull)
{
string shippingLabel = new OrderRepository().GetOrderReturnShippingLabel(orderId);
if (!string.IsNullOrEmpty(shippingLabel))
{
byte[] bytes = Convert.FromBase64String(shippingLabel);
Image image = null;
MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);
ms.Write(bytes, 0, bytes.Length);
image = Image.FromStream(ms, true);
image.RotateFlip(RotateFlipType.Rotate90FlipNone);
if (!showFull)
image = image.GetThumbnailImage(326, 570, null, IntPtr.Zero);
ImageConverter converter = new ImageConverter();
byte[] imgArray = (byte[])converter.ConvertTo(image, typeof(byte[]));
return File(imgArray.ToArray(), "image/gif");
}
else
{
return null;
}
}

An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll

I'm trying to loop through all the images I load, I'm able to process up to 40 images, then the I get the Out of memory error although I'm disposing the variable tempImage. The code breaks at the "Bitmap tempImage = new Bitmap(fileName);" line, plz help!
Is there a way to handle large number of input files? Like batch process and splitting the process into chunks? Because the operation would last more than a minute to finish, the program would crash by then.
foreach (string fileName in openFileDialog.FileNames)
{
circleDetection examDetect = new circleDetection();
Bitmap tempImage = new Bitmap(fileName);
directory.Text = fileName;
PictureBox picBox = new PictureBox();
picBox.Width = 200;
picBox.Height = 200;
picBox.Location = new System.Drawing.Point(picBoard.Controls.Count * (picBox.Width + 5) + 5, 5);
picBox.SizeMode = PictureBoxSizeMode.Zoom;
picBox.BorderStyle = BorderStyle.FixedSingle;
examDetect.ProcessImage(tempImage);
picBox.Image = examDetect.getImage();
Console.WriteLine(i++);
student.Add(compare(examDetect));
picBoard.Controls.Add(picBox);
tempImage.Dispose();
}
I prefer to use using() instead of .Dispose(). It's also disposing an object after it's cycle ended. So maybe you should try smth like next?
foreach (string fileName in openFileDialog.FileNames)
{
circleDetection examDetect = new circleDetection();
using (Bitmap tempImage = new Bitmap(fileName))
{
Exam.Add(tempImage);
directory.Text = fileName;
PictureBox picBox = new PictureBox();
picBox.Width = 200;
picBox.Height = 200;
picBox.Location = new System.Drawing.Point(picBoard.Controls.Count * (picBox.Width + 5) + 5, 5);
picBox.SizeMode = PictureBoxSizeMode.Zoom;
picBox.BorderStyle = BorderStyle.FixedSingle;
examDetect.ProcessImage(tempImage);
picBox.Image = examDetect.getImage();
Console.WriteLine(i++);
student.Add(compare(examDetect));
picBoard.Controls.Add(picBox);
}
}
See MSDN for more information about using statement.
UPDATE:
But, why don't you use stream for loading your files? It's recommended to use stream for such cases.

Release a file being used by another process

I'm implementing "copy map to clip" as an image.
what I do is I create an image an save it to certain directory and copy it to clipboard.
but before I do that I delete every file that exist in the directory, but now I cant since, the image is being used by clipboard.
here's my code.
public override void OnClick()
{
//base.OnClick();
DeleteOldCopiedJPG();
System.Windows.Forms.Clipboard.Clear();
string fileName = System.Windows.Forms.Application.ExecutablePath.Substring(0, System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\\") + 1) + Guid.NewGuid() + ".jpg";
//System.Windows.Forms.Cursor.Current = Cursors.Wait;
//if (System.IO.File.Exists(fileName))
// System.IO.File.Delete(fileName);
IExport objExport = (IExport)new ExportJPEG();
objExport.ExportFileName = fileName;
#if Debug || Release
ESRI.ArcGIS.Display.tagRECT objExportRECT = default( ESRI.ArcGIS.Display.tagRECT);
#else
tagRECT objExportRECT = default(tagRECT);
#endif
var _with1 = objExportRECT;
_with1.left = mapControl.ActiveView.ExportFrame.left;
_with1.top = mapControl.ActiveView.ExportFrame.top;
_with1.right = mapControl.ActiveView.ExportFrame.right;
_with1.bottom = mapControl.ActiveView.ExportFrame.bottom;
IEnvelope envelope = new EnvelopeClass();
envelope.PutCoords(mapControl.ActiveView.ExportFrame.left, mapControl.ActiveView.ExportFrame.top,
mapControl.ActiveView.ExportFrame.right, mapControl.ActiveView.ExportFrame.bottom);
objExport.PixelBounds = envelope;
System.Int32 intHDC = objExport.StartExporting();
mapControl.ActiveView.Output(intHDC, Convert.ToInt16(objExport.Resolution), objExportRECT, null, null);
objExport.FinishExporting();
objExport.Cleanup();
System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(fileName);
System.Windows.Forms.Clipboard.SetImage(objImage);
//RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
//renderTargetBitmap.Render((Visual)mapControl.ActiveView.ScreenDisplay);
//Clipboard.SetImage(renderTargetBitmap);
}
private void DeleteOldCopiedJPG(string Path)
{
string[] filePaths = Directory.GetFiles(System.Windows.Forms.Application.ExecutablePath.Substring(0, System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\\") + 1));
foreach (string filepath in filePaths)
if (filepath.Substring(filepath.Length - 4) == ".jpg")
try{ File.Delete(filepath); } catch {}
}
You need to dispose the image after inserting it into the clipboard:
using (System.Drawing.Image objImage = System.Drawing.Bitmap.FromFile(fileName))
{
System.Windows.Forms.Clipboard.SetImage(objImage);
}
Otherwise it will remain open until the garbage collector calls the finalizer for objImage.

Categories