Image Resize and Crop, am I on the right track?
-
I'm not sure that's an appropriate title but I can't think of a better one. So far I have the image resizing with the line:
imageBmp = New Bitmap(imageBmp, New Size(imageWidth, imageHeight))
To me that feels sloppy, and so far I can't figure out a nice way to make a single like way to crop, and if I do I feel it will be just as bad. Am I on the wrong track? Am I just over worrying about something simple that won't save/waste processor time/power? Would it be better for me to do both at once (resize and crop) by making a second image and using DrawImage like in the code below (currently it just crops)?imageNew As Bitmap = new Bitmap(Width, Height) imageNewGraphic As Graphics = Graphics.FromImage(imageNew) imageNewGraphic.DrawImage(imgBmp, new Rectangle(0, 0, Width, Height), X, Y, Width, Height, GraphicsUnit.Pixel)
I think I explained this well, I know there are other codes out there I could copy and I know that I'm reinventing the wheel, but I find I learn better this way. I'm very know to VB.NET but have had other programing training. Also this will be output to the web via ASP.NET usingimageBmp.Save(Response.OutputStream
, I don't believe that matters but I wanted to mention it anyway. Thanks for any help provided. -
I'm not sure that's an appropriate title but I can't think of a better one. So far I have the image resizing with the line:
imageBmp = New Bitmap(imageBmp, New Size(imageWidth, imageHeight))
To me that feels sloppy, and so far I can't figure out a nice way to make a single like way to crop, and if I do I feel it will be just as bad. Am I on the wrong track? Am I just over worrying about something simple that won't save/waste processor time/power? Would it be better for me to do both at once (resize and crop) by making a second image and using DrawImage like in the code below (currently it just crops)?imageNew As Bitmap = new Bitmap(Width, Height) imageNewGraphic As Graphics = Graphics.FromImage(imageNew) imageNewGraphic.DrawImage(imgBmp, new Rectangle(0, 0, Width, Height), X, Y, Width, Height, GraphicsUnit.Pixel)
I think I explained this well, I know there are other codes out there I could copy and I know that I'm reinventing the wheel, but I find I learn better this way. I'm very know to VB.NET but have had other programing training. Also this will be output to the web via ASP.NET usingimageBmp.Save(Response.OutputStream
, I don't believe that matters but I wanted to mention it anyway. Thanks for any help provided.RX Maverick wrote:
imageBmp = New Bitmap(imageBmp, New Size(imageWidth, imageHeight)) To me that feels sloppy
No, that's what you have to do.
RX Maverick wrote:
and so far I can't figure out a nice way to make a single like way to crop,
I think you're asking if there is a "single LINE" way to crop an image. No, there isn't. You have to create a new bitmap the size you want the cropped image to be, then copy the bits from the old bitmap to the new one.
RX Maverick wrote:
Would it be better for me to do both at once (resize and crop) by making a second image and using DrawImage like in the code below (currently it just crops)?
You could do it in one operation, but I wouldn't for right now. Get your app to work the way you want first, without worrying about wasting a ton of resources. If you're not constantly resizing and cropping an image, then this code should work just fine. It's one image operation. You're not really taxing the processor at all doing this.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007