one last quick question, is your code sample straight from their documentations, or did you write it based on their info? I am just worried its something else I have to learn (although that might be easier than learning to hard code it
Ooops, I missed the var declaration.
Just add the keyword "var" before the variable "filter" is initialized.
Here is the corrected (and tested) code:
using (var sourceImage = CodeCarvings.Piczard.ImageArchiver.LoadImage("~/Source.jpg"))
{
if (sourceImage.Size.Width == sourceImage.Size.Height)
{
// Just copy the file
System.IO.File.Copy(Server.MapPath("~/Source.jpg"), Server.MapPath("~/Dest.jpg"));
}
else
{
// Calculate the square size
int imageSize = sourceImage.Size.Width > sourceImage.Size.Height ? sourceImage.Size.Width : sourceImage.Size.Height;
// Get a fixed resize filter (square)
var filter = new CodeCarvings.Piczard.FixedResizeConstraint(imageSize, imageSize);
// Force white background
filter.CanvasColor = CodeCarvings.Piczard.BackgroundColor.GetStatic(System.Drawing.Color.White);
// Process the image (output = JPEG - 82% quality)
filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest.jpg", new CodeCarvings.Piczard.JpegFormatEncoderParams(82));
}
}
Hi Sorry to trouble you, but I am not sure I understand the docs.
I now need to loop through the square image and resize it to smaller images if the orginal image is big enough, but I don't know how to sort this out, any help would be appriciated
The code to use is not much different from the previous one.
You only need to check that the value of the variable imageSize is not greater than the desired value.
Example:
...
int imageSize = sourceImage.Size.Width > sourceImage.Size.Height ? sourceImage.Size.Width : sourceImage.Size.Height;
if (imageSize > 300)
{
// Max image size is 300x300 pixels
imageSize = 300;
}
...
Here is another example:
// Pass null as "maxSize" parameter if you don't need to resize big images.
protected void ToSquareImage(string sourceImageFilePath, string destImageFilePath, int? maxSize)
{
using (var sourceImage = CodeCarvings.Piczard.ImageArchiver.LoadImage(sourceImageFilePath))
{
if ((sourceImage.Size.Width == sourceImage.Size.Height) && ((!maxSize.HasValue) || ((maxSize.HasValue) && (sourceImage.Size.Width <= maxSize.Value))))
{
// Already square image AND (do not use MaxSize /OR/ size is <= maxSize)
// Just copy the file
System.IO.File.Copy(sourceImageFilePath, destImageFilePath);
}
else
{
// Calculate the square size
int imageSize = sourceImage.Size.Width > sourceImage.Size.Height ? sourceImage.Size.Width : sourceImage.Size.Height;
if (maxSize.HasValue)
{
// maxSize Provided
if (imageSize > maxSize.Value)
{
// Use maxSize
imageSize = maxSize.Value;
}
}
// Get a fixed resize filter (square)
var filter = new CodeCarvings.Piczard.FixedResizeConstraint(imageSize, imageSize);
// Force white background
filter.CanvasColor = CodeCarvings.Piczard.BackgroundColor.GetStatic();
// Process the image
filter.SaveProcessedImageToFileSystem(sourceImage, destImageFilePath);
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Loop through images in a folder
string sourceFolderPath = Server.MapPath("~/MySoyrceDirectory/");
string destFolderPath = Server.MapPath("~/MyDestinationDirectory/");
string[] sourceImages = System.IO.Directory.GetFiles(sourceFolderPath);
for (int i = 0; i < sourceImages.Length; i++)
{
// Calculate source and destination file paths
string sourceFilePath = sourceImages[i];
string sourceFileName = System.IO.Path.GetFileName(sourceFilePath);
string destFilePath = System.IO.Path.Combine(destFolderPath, sourceFileName);
// Process the image
ToSquareImage(sourceFilePath, destFilePath, 300);
}
}
This code loops through images in a folder and saves square images in another directory (MAX size is 300x300 pixels)
Thanks for that, but I don't want to different images, I want to upload 1 image, save the original in 1 folder, then make it a square and save it as a 1000x1000, then save it as 700x700, then save it as 300x300, then save it as 100x100, your current code
seems to make me re upload the image every time, and I wanted to use the previous image to make it smaller, and hopefull save processing and uploading.
Also I need to have a model to allow the image to be uploaded, do I need a string with your method or do I need HttpPostedFileBase?
No, you absolutely don't need to reupload the image every time.
This is another example that generates 5 images from 1 source (square, 1000x1000, 700x700, etc...):
using (var sourceImage = CodeCarvings.Piczard.ImageArchiver.LoadImage("~/Source.jpg"))
{
// Calculate the square size
int imageSize = sourceImage.Size.Width > sourceImage.Size.Height ? sourceImage.Size.Width : sourceImage.Size.Height;
// Get a fixed resize filter (square) - Background color: White
var filter = new CodeCarvings.Piczard.FixedResizeConstraint(imageSize, imageSize);
filter.CanvasColor = CodeCarvings.Piczard.BackgroundColor.GetStatic(System.Drawing.Color.White);
// Process the image (output = JPEG - 82% quality)
filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest.jpg", new CodeCarvings.Piczard.JpegFormatEncoderParams(82));
// Save the 1000x1000 image
filter.Size = new System.Drawing.SizeF(1000, 1000);
filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest_1000x1000.jpg");
// Save the 700x700 image
filter.Size = new System.Drawing.SizeF(700, 700);
filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest_700x700.jpg");
// Save the 300x300 image
filter.Size = new System.Drawing.SizeF(300, 300);
filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest_300x300.jpg");
// Save the 100x100 image
filter.Size = new System.Drawing.SizeF(100, 100);
filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest_100x100.jpg");
}
Regarding your second question, you need HttpPostedFileBase.
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Image Cropping rectangle to square
Apr 25, 2012 03:29 PM|LINK
one last quick question, is your code sample straight from their documentations, or did you write it based on their info? I am just worried its something else I have to learn (although that might be easier than learning to hard code it
zioturo
Member
129 Points
43 Posts
Re: Image Cropping rectangle to square
Apr 25, 2012 04:00 PM|LINK
Neither of these, I'm the developer who created Piczard.
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Image Cropping rectangle to square
Apr 25, 2012 09:00 PM|LINK
nice, i was just wondering how easy it is for a relative noobie to get to grips with things like this, as it looks very interesting
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Image Cropping rectangle to square
May 24, 2012 01:04 PM|LINK
Hi
Sorry for this but my code isn't recognising the filter, as filer isn't set to an object
zioturo
Member
129 Points
43 Posts
Re: Image Cropping rectangle to square
May 24, 2012 01:23 PM|LINK
Hi,
Ooops, I missed the var declaration.
Just add the keyword "var" before the variable "filter" is initialized.
Here is the corrected (and tested) code:
using (var sourceImage = CodeCarvings.Piczard.ImageArchiver.LoadImage("~/Source.jpg")) { if (sourceImage.Size.Width == sourceImage.Size.Height) { // Just copy the file System.IO.File.Copy(Server.MapPath("~/Source.jpg"), Server.MapPath("~/Dest.jpg")); } else { // Calculate the square size int imageSize = sourceImage.Size.Width > sourceImage.Size.Height ? sourceImage.Size.Width : sourceImage.Size.Height; // Get a fixed resize filter (square) var filter = new CodeCarvings.Piczard.FixedResizeConstraint(imageSize, imageSize); // Force white background filter.CanvasColor = CodeCarvings.Piczard.BackgroundColor.GetStatic(System.Drawing.Color.White); // Process the image (output = JPEG - 82% quality) filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest.jpg", new CodeCarvings.Piczard.JpegFormatEncoderParams(82)); } }Regards
Sergio
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Image Cropping rectangle to square
May 24, 2012 02:43 PM|LINK
lol sorry I should have got that, many thanks
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Image Cropping rectangle to square
Jun 07, 2012 10:31 PM|LINK
Hi Sorry to trouble you, but I am not sure I understand the docs.
I now need to loop through the square image and resize it to smaller images if the orginal image is big enough, but I don't know how to sort this out, any help would be appriciated
zioturo
Member
129 Points
43 Posts
Re: Image Cropping rectangle to square
Jun 08, 2012 03:23 PM|LINK
The code to use is not much different from the previous one.
You only need to check that the value of the variable imageSize is not greater than the desired value.
Example:
... int imageSize = sourceImage.Size.Width > sourceImage.Size.Height ? sourceImage.Size.Width : sourceImage.Size.Height; if (imageSize > 300) { // Max image size is 300x300 pixels imageSize = 300; } ...Here is another example:
// Pass null as "maxSize" parameter if you don't need to resize big images. protected void ToSquareImage(string sourceImageFilePath, string destImageFilePath, int? maxSize) { using (var sourceImage = CodeCarvings.Piczard.ImageArchiver.LoadImage(sourceImageFilePath)) { if ((sourceImage.Size.Width == sourceImage.Size.Height) && ((!maxSize.HasValue) || ((maxSize.HasValue) && (sourceImage.Size.Width <= maxSize.Value)))) { // Already square image AND (do not use MaxSize /OR/ size is <= maxSize) // Just copy the file System.IO.File.Copy(sourceImageFilePath, destImageFilePath); } else { // Calculate the square size int imageSize = sourceImage.Size.Width > sourceImage.Size.Height ? sourceImage.Size.Width : sourceImage.Size.Height; if (maxSize.HasValue) { // maxSize Provided if (imageSize > maxSize.Value) { // Use maxSize imageSize = maxSize.Value; } } // Get a fixed resize filter (square) var filter = new CodeCarvings.Piczard.FixedResizeConstraint(imageSize, imageSize); // Force white background filter.CanvasColor = CodeCarvings.Piczard.BackgroundColor.GetStatic(); // Process the image filter.SaveProcessedImageToFileSystem(sourceImage, destImageFilePath); } } } protected void Page_Load(object sender, EventArgs e) { // Loop through images in a folder string sourceFolderPath = Server.MapPath("~/MySoyrceDirectory/"); string destFolderPath = Server.MapPath("~/MyDestinationDirectory/"); string[] sourceImages = System.IO.Directory.GetFiles(sourceFolderPath); for (int i = 0; i < sourceImages.Length; i++) { // Calculate source and destination file paths string sourceFilePath = sourceImages[i]; string sourceFileName = System.IO.Path.GetFileName(sourceFilePath); string destFilePath = System.IO.Path.Combine(destFolderPath, sourceFileName); // Process the image ToSquareImage(sourceFilePath, destFilePath, 300); } }This code loops through images in a folder and saves square images in another directory (MAX size is 300x300 pixels)
Have a nice day
Sergio Turolla
EnenDaveyBoy
Participant
1466 Points
1157 Posts
Re: Image Cropping rectangle to square
Jun 09, 2012 03:39 PM|LINK
Thanks for that, but I don't want to different images, I want to upload 1 image, save the original in 1 folder, then make it a square and save it as a 1000x1000, then save it as 700x700, then save it as 300x300, then save it as 100x100, your current code seems to make me re upload the image every time, and I wanted to use the previous image to make it smaller, and hopefull save processing and uploading.
Also I need to have a model to allow the image to be uploaded, do I need a string with your method or do I need HttpPostedFileBase?
zioturo
Member
129 Points
43 Posts
Re: Image Cropping rectangle to square
Jun 11, 2012 10:02 AM|LINK
No, you absolutely don't need to reupload the image every time.
This is another example that generates 5 images from 1 source (square, 1000x1000, 700x700, etc...):
using (var sourceImage = CodeCarvings.Piczard.ImageArchiver.LoadImage("~/Source.jpg")) { // Calculate the square size int imageSize = sourceImage.Size.Width > sourceImage.Size.Height ? sourceImage.Size.Width : sourceImage.Size.Height; // Get a fixed resize filter (square) - Background color: White var filter = new CodeCarvings.Piczard.FixedResizeConstraint(imageSize, imageSize); filter.CanvasColor = CodeCarvings.Piczard.BackgroundColor.GetStatic(System.Drawing.Color.White); // Process the image (output = JPEG - 82% quality) filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest.jpg", new CodeCarvings.Piczard.JpegFormatEncoderParams(82)); // Save the 1000x1000 image filter.Size = new System.Drawing.SizeF(1000, 1000); filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest_1000x1000.jpg"); // Save the 700x700 image filter.Size = new System.Drawing.SizeF(700, 700); filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest_700x700.jpg"); // Save the 300x300 image filter.Size = new System.Drawing.SizeF(300, 300); filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest_300x300.jpg"); // Save the 100x100 image filter.Size = new System.Drawing.SizeF(100, 100); filter.SaveProcessedImageToFileSystem(sourceImage, "~/Dest_100x100.jpg"); }Regarding your second question, you need HttpPostedFileBase.