Ver Mensaje Individual
  #5 (permalink)  
Antiguo 24/07/2008, 02:44
Ciberyo
 
Fecha de Ingreso: diciembre-2007
Mensajes: 36
Antigüedad: 17 años, 2 meses
Puntos: 0
Respuesta: Galeria de imagenes

Vale ya esta listo el crop (en teoria las miniaturas no son iguales y con la funcion que pongo ahora las recorta ahora al mismo tamaño)
Código:
//Donde acaba  el codigo anterior poneis esto
 myBitmap= ImageCrop(myBitmap, 173, 234, AnchorPosition.Center);
Despues añadis la enumeracion

Código:
  public enum AnchorPosition { Top, Center, Bottom, Left, Right }
y el procedimiento para realziar el crop

Código:
    
public static System.Drawing.Bitmap ImageCrop(System.Drawing.Bitmap imgPhoto,
                                                        int Width, int Height,  AnchorPosition Anchor)
    {
        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;
        int sourceX = 0;
        int sourceY = 0;
        int destX = 0;
        int destY = 0;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;

        nPercentW = ((float)Width / (float)sourceWidth);
        nPercentH = ((float)Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
        {
            nPercent = nPercentW;
            switch (Anchor)
            {
                case AnchorPosition.Top: 
                    destY = 0;
                    break;
                case AnchorPosition.Bottom:
                    destY = (int)(Height - (sourceHeight * nPercent));
                    break;
                default:
                    destY = (int)((Height - (sourceHeight * nPercent)) / 2);
                    break;
            }
        }
        else
        {
            nPercent = nPercentH;
            switch (Anchor)
            {
                case AnchorPosition.Left:
                    destX = 0;
                    break;
                case AnchorPosition.Right:
                    destX = (int)(Width - (sourceWidth * nPercent));
                    break;
                default:
                    destX = (int)((Width - (sourceWidth * nPercent)) / 2);
                    break;
            }

        }

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap bmPhoto = new Bitmap(Width, Height,
            PixelFormat.Format24bppRgb);
        bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
            imgPhoto.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        return bmPhoto;
    }

}