-
Jul 6th, 2018, 02:55 PM
#1
Thread Starter
New Member
I have a question about how crop Image
I found many explanations in crop Image
but they all rotate to crop one image at a time
If I have a large number of pictures to crop by the same measurement
What advice do you have to edit on this code so that it will be executed once or automatically
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
public partial class CropAndSaveImageCSharp : System.Web.UI.Page
{
protected void btnUpload_Click(object sender, EventArgs e)
{
}
string fileName = string.Empty;
string filePath = string.Empty;
string extension = string.Empty;
try
{
//Check if Fileupload control has file in it
if (FileUpload1.HasFile)
{
// Get selected image extension
extension = Path.GetExtension(FileUpload1.FileName).ToLower();
//Check image is of valid type or not
if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".gif" || extension == ".bmp")
{
//Cretae unique name for the file
fileName = Guid.NewGuid().ToString() + extension;
//Create path for the image to store
filePath = Path.Combine(Server.MapPath("~/Images"), fileName);
//Save image in folder
FileUpload1.SaveAs(filePath);
//Show the panel and load the uploaded image in image control.
pnlCrop.Visible = true;
imgToCrop.ImageUrl = "~/Images/" + fileName;
}
else
{
lblMsg.Text = "Please select jpg, jpeg, png, gif or bmp file only";
}
}
else
{
lblMsg.Text = "Please select file to upload";
}
}
catch (Exception ex)
{
lblMsg.Text = "Oops!! error occured : " + ex.Message.ToString();
}
finally
{
extension = string.Empty;
fileName = string.Empty;
filePath = string.Empty;
}
}
protected void btnCrop_Click(object sender, EventArgs e)
{
string croppedFileName = string.Empty;
string croppedFilePath = string.Empty;
//get uploaded image name
string fileName = Path.GetFileName(imgToCrop.ImageUrl);
//get uploaded image path
string filePath = Path.Combine(Server.MapPath("~/Images"), fileName);
//Check if file exists on the path i.e. in the UploadedImages folder.
if (File.Exists(filePath))
{
//Get the image from UploadedImages folder.
System.Drawing.Image orgImg = System.Drawing.Image.FromFile(filePath);
//Get user selected cropped area
//Convert.ToInt32(String.Format("{0:0.##}", YCoordinate.Value)),
Rectangle areaToCrop = new Rectangle(
Convert.ToInt32(XCoordinate.Value),
Convert.ToInt32(YCoordinate.Value),
Convert.ToInt32(Width.Value),
Convert.ToInt32(Height.Value));
try
{
Bitmap bitMap = new Bitmap(areaToCrop.Width, areaToCrop.Height);
//Create graphics object for alteration
using (Graphics g = Graphics.FromImage(bitMap))
{
//Draw image to screen
g.DrawImage(orgImg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), areaToCrop, GraphicsUnit.Pixel);
}
//name the cropped image
croppedFileName = "crop_" + fileName;
//Create path to store the cropped image
croppedFilePath = Path.Combine(Server.MapPath("~/Images"), croppedFileName);
//save cropped image in folder
bitMap.Save(croppedFilePath);
orgImg.Dispose();
bitMap = null;
//Now you can delete the original uploaded image from folder
File.Delete(filePath);
//Hide the panel
pnlCrop.Visible = false;
//Show success message in label
lblMsg.ForeColor = Color.Green;
lblMsg.Text = "Image cropped and saved successfully";
//Show cropped image
imgCropped.ImageUrl = "~/Images/" + croppedFileName;
//Show Reset button
btnReset.Visible = true;
}
catch (Exception ex)
{
lblMsg.Text = "Oops!! error occured : " + ex.Message.ToString();
}
finally
{
fileName = string.Empty;
filePath = string.Empty;
croppedFileName = string.Empty;
croppedFilePath = string.Empty;
}
}
}
protected void btnReset_Click(object sender, EventArgs e)
{
imgCropped.ImageUrl = "";
lblMsg.Text = string.Empty;
btnReset.Visible = false;
}
}
-
Jul 7th, 2018, 01:47 AM
#2
Thread Starter
New Member
Re: I have a question about how crop Image
This is the project
Please help modify it
If I have a large collection of Image and I want to make crop and save
Is there a way to do that?
So that they are cut once with the same measurements
Last edited by kha.kha; Jul 20th, 2018 at 11:55 AM.
-
Jul 10th, 2018, 12:08 PM
#3
Thread Starter
New Member
Re: I have a question about how crop Image
Hello owners
Now 3 days ..... I am waiting for any comment
Is my question unclear or what?
-
Jul 11th, 2018, 10:37 AM
#4
Re: I have a question about how crop Image
-
Jul 11th, 2018, 10:42 PM
#5
Re: I have a question about how crop Image
Originally Posted by kha.kha
If I have a large number of pictures to crop by the same measurement
What advice do you have to edit on this code so that it will be executed once or automatically
Put all your images into a list and then call Parallel.ForEach to process multiple items in parallel. As a simple example, this:
csharp Code:
var imageFilePaths = new List<string>(); // ... foreach (var imageFilePath in imageFilePaths) { ProcessImageFile(imageFilePath); }
becomes this:
csharp Code:
var imageFilePaths = new List<string>(); // ... Parallel.ForEach(imageFilePaths, ProcessImageFile);
-
Jul 11th, 2018, 10:47 PM
#6
Re: I have a question about how crop Image
I think I may have misunderstood the question but it's still a good idea to use parallel processing to speed things up. The thing is, I'm not sure why the question is being asked in the first place. If you have a method that crops an image in a specific way then you simply call that method for multiple images, whether in serial or parallel, and they will all be cropped in that specific way. I'm not seeing what the actual problem is.
Maybe you didn't actually think to move the cropping code to its own method. If not, that's your solution. You can do that in your existing single-image code first, to make sure that it works as expected. You can then simply call that same method multiple times for multiple images.
-
Jul 12th, 2018, 09:40 AM
#7
Thread Starter
New Member
Re: I have a question about how crop Image
In the beginning
Thank you, Mr. #jdc2000
But this is not the problem .... The problem is how how crop all Images (same size crop on all images)
and Thank you, Mr. #jmcilhinney
I will explain the idea again
?????????????????
If I have a lot of bills..... like this
and I want to crop some information out of it as Image
example, shipping information
How do I do it at one time for all images and same size crop
@jdc2000 @jmcilhinney
-
Jul 12th, 2018, 10:22 AM
#8
Re: I have a question about how crop Image
It seems like you ignored what I did say. If you can already do this for one image then you basically have all you need. Just refactor your code so that the code that does the cropping is in its own method and you just pass in what it needs: either the Image itself or the path of an image file. Once that is working with your current single image, you can do as I showed and call that method in a loop to process multiple images. You can use a regular loop and process the images in serial or call Parallel.ForEach and process them in parallel. I suggest that you follow those instructions and post back if you encounter any specific issues along the way.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|