I have an image upload form that stores two sizes of images: Large and Thumbnail image.
我有一个图像上传表格,存储两种尺寸的图像:大图像和缩略图图像。
For the thumbnail image, I am trying to crop image from the center and re-size to: 30px x 30px.
对于缩略图图像,我试图从中心裁剪图像并重新调整大小为:30px x 30px。
Here is my code:
这是我的代码:
private static Bitmap ResizeImage(MemoryStream uploadStream, int maxWidth, int maxHeight)
{
Image img = Image.FromStream(uploadStream);
double ratioX = (double)maxWidth / img.Width;
double ratioY = (double)maxHeight / img.Height;
double ratio = Math.Max(ratioX, ratioY);
int newWidth = (int)(img.Width * ratio);
int newHeight = (int)(img.Height * ratio);
Bitmap resizedBitmap = new Bitmap(newWidth, newHeight);
Graphics.FromImage(resizedBitmap).DrawImage(img, 0, 0, newWidth, newHeight);
img.Dispose();
return resizedBitmap;
}
private static Bitmap CropImageToCentre(MemoryStream uploadStream, int width, int height)
{
Image img = Image.FromStream(uploadStream);
Bitmap resizedBitmap = new Bitmap(img);
int StartX = 0, StartY = 0;
int EndX = img.Width, EndY = img.Height;
bool Crop = false;
if (img.Width > width)
{
int MidX = img.Width / 2;
StartX = MidX - (width / 2);
EndX = MidX + (width / 2);
Crop = true;
}
if (img.Width > height)
{
int MidY = img.Height / 2;
StartY = MidY - (height / 2);
EndY = MidY + (height / 2);
Crop = true;
}
if (Crop)
{
Size imgSize = new Size(width, height);
resizedBitmap = new Bitmap(img, imgSize);
}
img.Dispose();
return resizedBitmap;
}
public static Bitmap ResizeThumbnail(MemoryStream ms)
{
int thumbWidth = int.Parse(ConfigurationManager.AppSettings["thumbwidth"]);
int thumbHeight = int.Parse(ConfigurationManager.AppSettings["thumbheight"]);
return CropImageToCentre(BitmapToMemoryStream(ResizeImage(ms, thumbWidth, thumbHeight)), thumbWidth, thumbHeight);
}
public static Bitmap ResizeLargeImage(MemoryStream ms)
{
int width = int.Parse(ConfigurationManager.AppSettings["largewidth"]);
int height = int.Parse(ConfigurationManager.AppSettings["largeheight"]);
return ResizeImage(ms, width, height);
}
private static MemoryStream BitmapToMemoryStream(Bitmap bm)
{
MemoryStream memoryStream = new MemoryStream();
bm.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
return memoryStream;
}
The issue I'm having is when calling the ResizeThumbnail()
method, the image does not get cropped or resized to 30px height and width.
我遇到的问题是在调用ResizeThumbnail()方法时,图像不会被裁剪或调整为30px的高度和宽度。
2 个解决方案
#1
1
Found the issue. I've updated the CropImageToCentre
method to the following:
发现了这个问题。我已将CropImageToCentre方法更新为以下内容:
private static Bitmap CropImageToCentre(MemoryStream uploadStream, int width, int height)
{
Image img = Image.FromStream(uploadStream);
Bitmap resizedBitmap = new Bitmap(img);
int StartX = 0, StartY = 0;
int EndX = img.Width, EndY = img.Height;
bool Crop = false;
if (img.Width > width)
{
int MidX = img.Width / 2;
StartX = MidX - (width / 2);
EndX = MidX + (width / 2);
Crop = true;
}
if (img.Height > height)
{
int MidY = img.Height / 2;
StartY = MidY - (height / 2);
EndY = MidY + (height / 2);
Crop = true;
}
if (Crop)
{
Bitmap cropped = new Bitmap(width, height);
Graphics.FromImage(cropped).DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(StartX, StartY, EndX, EndY), GraphicsUnit.Pixel);
resizedBitmap = cropped;
}
return resizedBitmap;
}
#2
0
there can be multiple solutions one could be like this see this function
可以有多个解决方案可以像这样看到这个功能
private Size getScaledImageDimensions(int currentImageWidth,int currentImageHeight,
int desiredImageWidth,int desiredImageHeight)
{
/* First, we must calculate a multiplier that will be used
* to get the dimensions of the new, scaled image.
*/
double scaleImageMultiplier = 0;
/* This multiplier is defined as the ratio of the
* Desired Dimension to the Current Dimension.
* Specifically which dimension is used depends on the larger
* dimension of the image, as this will be the constraining dimension
* when we fit to the window.
*/
/* Determine if Image is Portrait or Landscape. */
if (currentImageHeight > currentImageWidth) /* Image is Portrait */
{
/* Calculate the multiplier based on the heights. */
if (desiredImageHeight > desiredImageWidth)
{
scaleImageMultiplier = (double)desiredImageWidth / (double)currentImageWidth;
}
else
{
scaleImageMultiplier = (double)desiredImageHeight / (double)currentImageHeight;
}
}
else /* Image is Landscape */
{
/* Calculate the multiplier based on the widths. */
if (desiredImageHeight >= desiredImageWidth)
{
scaleImageMultiplier = (double)desiredImageWidth / (double)currentImageWidth;
}
else
{
scaleImageMultiplier = (double)desiredImageHeight / (double)currentImageHeight;
}
}
/* Generate and return the new scaled dimensions.
* Essentially, we multiply each dimension of the original image
* by the multiplier calculated above to yield the dimensions
* of the scaled image. The scaled image can be larger or smaller
* than the original.
*/
return new Size(
(int)(currentImageWidth * scaleImageMultiplier),
(int)(currentImageHeight * scaleImageMultiplier));
}//end of fun
// you can call it like this
{
Image tempImage = (Image)<someImageSource....>;
/* Calculate the dimensions necessary for an image to fit. */
Size fitImageSize = this.getScaledImageDimensions(
current_Width, current_Height, desired_Width, desired_Height);
Bitmap imgOutput = new Bitmap(tempImage, fitImageSize.Width, fitImageSize.Height);
}
//end of code worked for me sry not good identation
//为我工作的代码结束不是很好的认同
#1
1
Found the issue. I've updated the CropImageToCentre
method to the following:
发现了这个问题。我已将CropImageToCentre方法更新为以下内容:
private static Bitmap CropImageToCentre(MemoryStream uploadStream, int width, int height)
{
Image img = Image.FromStream(uploadStream);
Bitmap resizedBitmap = new Bitmap(img);
int StartX = 0, StartY = 0;
int EndX = img.Width, EndY = img.Height;
bool Crop = false;
if (img.Width > width)
{
int MidX = img.Width / 2;
StartX = MidX - (width / 2);
EndX = MidX + (width / 2);
Crop = true;
}
if (img.Height > height)
{
int MidY = img.Height / 2;
StartY = MidY - (height / 2);
EndY = MidY + (height / 2);
Crop = true;
}
if (Crop)
{
Bitmap cropped = new Bitmap(width, height);
Graphics.FromImage(cropped).DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(StartX, StartY, EndX, EndY), GraphicsUnit.Pixel);
resizedBitmap = cropped;
}
return resizedBitmap;
}
#2
0
there can be multiple solutions one could be like this see this function
可以有多个解决方案可以像这样看到这个功能
private Size getScaledImageDimensions(int currentImageWidth,int currentImageHeight,
int desiredImageWidth,int desiredImageHeight)
{
/* First, we must calculate a multiplier that will be used
* to get the dimensions of the new, scaled image.
*/
double scaleImageMultiplier = 0;
/* This multiplier is defined as the ratio of the
* Desired Dimension to the Current Dimension.
* Specifically which dimension is used depends on the larger
* dimension of the image, as this will be the constraining dimension
* when we fit to the window.
*/
/* Determine if Image is Portrait or Landscape. */
if (currentImageHeight > currentImageWidth) /* Image is Portrait */
{
/* Calculate the multiplier based on the heights. */
if (desiredImageHeight > desiredImageWidth)
{
scaleImageMultiplier = (double)desiredImageWidth / (double)currentImageWidth;
}
else
{
scaleImageMultiplier = (double)desiredImageHeight / (double)currentImageHeight;
}
}
else /* Image is Landscape */
{
/* Calculate the multiplier based on the widths. */
if (desiredImageHeight >= desiredImageWidth)
{
scaleImageMultiplier = (double)desiredImageWidth / (double)currentImageWidth;
}
else
{
scaleImageMultiplier = (double)desiredImageHeight / (double)currentImageHeight;
}
}
/* Generate and return the new scaled dimensions.
* Essentially, we multiply each dimension of the original image
* by the multiplier calculated above to yield the dimensions
* of the scaled image. The scaled image can be larger or smaller
* than the original.
*/
return new Size(
(int)(currentImageWidth * scaleImageMultiplier),
(int)(currentImageHeight * scaleImageMultiplier));
}//end of fun
// you can call it like this
{
Image tempImage = (Image)<someImageSource....>;
/* Calculate the dimensions necessary for an image to fit. */
Size fitImageSize = this.getScaledImageDimensions(
current_Width, current_Height, desired_Width, desired_Height);
Bitmap imgOutput = new Bitmap(tempImage, fitImageSize.Width, fitImageSize.Height);
}
//end of code worked for me sry not good identation
//为我工作的代码结束不是很好的认同