tailieunhanh - Cắt xén ảnh

Introduction In this article, I'll give a demo of how to crop an image by selecting a region with the mouse. | Cắt xén ảnh • Download source code - 158 KB (The images show the "home computers" in early days) Introduction In this article, I'll give a demo of how to crop an image by selecting a region with the mouse. Background We select a region of an image, create another image of the selected region, and zoom the new image to the size of the PictureBox. Although the PictureBox has its the SizeMode = , a MemoryOutOfRange exception may be thrown when cropping the image several times. So, I assume this property is just for fitting an image to a PictureBox once when loading the form. Extension methods for Image I've implemented the cropping and fitting to the PictureBox as an extension to the Image class. Cropping The image is cropped by cloning a region of the original image. Collapse /// /// Crops an image according to a selection rectangel /// /// /// the image to be cropped /// /// /// the selection /// /// /// cropped image /// public static Image Crop(this Image image, Rectangle selection) { Bitmap bmp = image as Bitmap; // Check if it is a bitmap: if (bmp == null) throw new ArgumentException("No valid bitmap"); // Crop the image: Bitmap cropBmp = (selection, ); // Release the resources: (); return cropBmp; } Fitting the image to the PictureBox As mentioned under the Background section, fitting can't be done by the PictureBox itself. So, I've coded this myself. As the first step, the scale factors in the vertical and horizontal directions are calculated. To scale (or zoom) the image so that the original ratio of width to height is maintained, the bigger ratio of the scale is used. The interpolation-mode is set to produce high quality pictures. Collapse /// /// Fits an image to the size of a picturebox /// /// /// image to be fit /// /// /// picturebox in that the image should fit /// /// /// fitted image /// /// .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.