comparison FBGUI/FBGUI.cs @ 33:e9b704f193df

FBGUI: Improved FBGImageBox
author Ivo Smits <Ivo@UCIS.nl>
date Fri, 19 Apr 2013 17:46:24 +0200
parents ff1bc8445743
children 70bde4fa6a2f
comparison
equal deleted inserted replaced
32:ff1bc8445743 33:e9b704f193df
1368 base.MouseDown(position, buttons); 1368 base.MouseDown(position, buttons);
1369 } 1369 }
1370 } 1370 }
1371 public class FBGImageBox : FBGControl { 1371 public class FBGImageBox : FBGControl {
1372 Image image = null; 1372 Image image = null;
1373 Image scaledImage = null;
1374 Size imageSize;
1375 Boolean ownsImage = false;
1373 PictureBoxSizeMode sizeMode = PictureBoxSizeMode.Normal; 1376 PictureBoxSizeMode sizeMode = PictureBoxSizeMode.Normal;
1374 Rectangle imageRect; 1377 Rectangle imageRect;
1375 public Image Image { get { return image; } set { image = value; UpdateImageRect(Size.Empty); } } 1378 public Image Image { get { return image; } set { SetImage(value, false); } }
1379 public Boolean PreScaleImage { get; set; }
1380 public void SetOwnedImage(Image img) { SetImage(img, true); }
1381 private void SetImage(Image img, Boolean owned) {
1382 image = img;
1383 imageSize = img == null ? Size.Empty : img.Size;
1384 ownsImage = owned;
1385 UpdateImageRect(Size.Empty);
1386 }
1376 public FBGImageBox(IFBGContainerControl parent) : base(parent) { } 1387 public FBGImageBox(IFBGContainerControl parent) : base(parent) { }
1377 public PictureBoxSizeMode SizeMode { get { return sizeMode; } set { sizeMode = value; UpdateImageRect(Size.Empty); } } 1388 public PictureBoxSizeMode SizeMode { get { return sizeMode; } set { sizeMode = value; UpdateImageRect(Size.Empty); } }
1378 public override Rectangle Bounds { 1389 public override Rectangle Bounds {
1379 get { 1390 get {
1380 return base.Bounds; 1391 return base.Bounds;
1381 } 1392 }
1382 set { 1393 set {
1383 UpdateImageRect(value.Size); 1394 if (Bounds.Size != value.Size) UpdateImageRect(value.Size);
1384 base.Bounds = value; 1395 base.Bounds = value;
1385 } 1396 }
1386 } 1397 }
1387 private void UpdateImageRect(Size csize) { 1398 private void UpdateImageRect(Size csize) {
1399 if (scaledImage != null && scaledImage != image) scaledImage.Dispose();
1400 scaledImage = null;
1388 if (image == null) return; 1401 if (image == null) return;
1389 Boolean boundsset = !csize.IsEmpty; 1402 Boolean boundsset = !csize.IsEmpty;
1390 if (!boundsset && sizeMode == PictureBoxSizeMode.AutoSize) { 1403 if (!boundsset && sizeMode == PictureBoxSizeMode.AutoSize) {
1391 Size = Image.Size; 1404 scaledImage = image;
1405 Size = imageSize;
1392 return; 1406 return;
1393 } 1407 }
1394 if (!boundsset) csize = Bounds.Size; 1408 if (!boundsset) csize = Bounds.Size;
1395 switch (sizeMode) { 1409 switch (sizeMode) {
1396 case PictureBoxSizeMode.AutoSize: 1410 case PictureBoxSizeMode.AutoSize:
1397 case PictureBoxSizeMode.Normal: 1411 case PictureBoxSizeMode.Normal:
1398 imageRect = new Rectangle(Point.Empty, image.Size); 1412 imageRect = new Rectangle(Point.Empty, imageSize);
1399 break; 1413 break;
1400 case PictureBoxSizeMode.CenterImage: 1414 case PictureBoxSizeMode.CenterImage:
1401 imageRect = new Rectangle(csize.Width / 2 - image.Width / 2, csize.Height / 2 - Image.Height / 2, image.Width, image.Height); 1415 imageRect = new Rectangle(csize.Width / 2 - imageSize.Width / 2, csize.Height / 2 - imageSize.Height / 2, imageSize.Width, imageSize.Height);
1402 break; 1416 break;
1403 case PictureBoxSizeMode.StretchImage: 1417 case PictureBoxSizeMode.StretchImage:
1404 imageRect = new Rectangle(Point.Empty, csize); 1418 imageRect = new Rectangle(Point.Empty, csize);
1405 break; 1419 break;
1406 case PictureBoxSizeMode.Zoom: 1420 case PictureBoxSizeMode.Zoom:
1407 float xrat = (float)csize.Width / (float)image.Width; 1421 float xrat = (float)csize.Width / (float)imageSize.Width;
1408 float yrat = (float)csize.Height / (float)image.Height; 1422 float yrat = (float)csize.Height / (float)imageSize.Height;
1409 float rat = Math.Min(xrat, yrat); 1423 float rat = Math.Min(xrat, yrat);
1410 SizeF dispsize = new SizeF(image.Width * rat, image.Height * rat); 1424 SizeF dispsize = new SizeF(imageSize.Width * rat, imageSize.Height * rat);
1411 imageRect = Rectangle.Round(new RectangleF(csize.Width / 2f - dispsize.Width / 2f, csize.Height / 2f - dispsize.Height / 2f, dispsize.Width, dispsize.Height)); 1425 imageRect = Rectangle.Round(new RectangleF(csize.Width / 2f - dispsize.Width / 2f, csize.Height / 2f - dispsize.Height / 2f, dispsize.Width, dispsize.Height));
1412 break; 1426 break;
1413 } 1427 }
1428 if (imageRect.Size == imageSize) scaledImage = image;
1414 if (!boundsset) Invalidate(); 1429 if (!boundsset) Invalidate();
1415 } 1430 }
1416 protected override void Paint(Graphics g) { 1431 protected override void Paint(Graphics g) {
1417 if (!Visible) return; 1432 if (!Visible) return;
1418 base.Paint(g); 1433 base.Paint(g);
1419 if (image != null) g.DrawImage(image, imageRect); 1434 if (PreScaleImage && scaledImage == null && image != null) scaledImage = new Bitmap(image, imageRect.Size);
1435 if (scaledImage != null) g.DrawImage(image, imageRect);
1420 } 1436 }
1421 public Point PointToImage(Point point) { 1437 public Point PointToImage(Point point) {
1422 switch (sizeMode) { 1438 switch (sizeMode) {
1423 case PictureBoxSizeMode.AutoSize: 1439 case PictureBoxSizeMode.AutoSize:
1424 case PictureBoxSizeMode.Normal: 1440 case PictureBoxSizeMode.Normal:
1428 point.Y -= imageRect.Y; 1444 point.Y -= imageRect.Y;
1429 break; 1445 break;
1430 case PictureBoxSizeMode.StretchImage: 1446 case PictureBoxSizeMode.StretchImage:
1431 case PictureBoxSizeMode.Zoom: 1447 case PictureBoxSizeMode.Zoom:
1432 default: 1448 default:
1433 point.X = (point.X - imageRect.X) * image.Width / imageRect.Width; 1449 point.X = (point.X - imageRect.X) * imageSize.Width / imageRect.Width;
1434 point.Y = (point.Y - imageRect.Y) * image.Height / imageRect.Height; 1450 point.Y = (point.Y - imageRect.Y) * imageSize.Height / imageRect.Height;
1435 break; 1451 break;
1436 } 1452 }
1437 return point; 1453 return point;
1454 }
1455 protected override void Orphaned() {
1456 base.Orphaned();
1457 if (scaledImage != null && scaledImage != image) scaledImage.Dispose();
1458 scaledImage = null;
1459 if (ownsImage) {
1460 image.Dispose();
1461 image = null;
1462 }
1438 } 1463 }
1439 } 1464 }
1440 public class FBGListBox : FBGControl { 1465 public class FBGListBox : FBGControl {
1441 private List<Object> items = new List<object>(); 1466 private List<Object> items = new List<object>();
1442 private Object selected = null; 1467 private Object selected = null;