อยากทราบว่าเราต้องเขียน Code อย่างไรครับ
เพราะต้องค่า PictureBox ให้ปรับรูปให้ได้ขนาดอัตโนมัติ อยากทราบว่าถ้าต้องการูปที่โชว์อยู่ใน PictureBox ลงใน DB Data Type แบบ image
private void DisplayImage(string ImagePath)
{
byte[] imageBytes = Security.DecryptStream(ImagePath, SecretKey);
Image myImage = Image.FromStream(new MemoryStream(imageBytes));
Size fitImageSize = ScaledImageDimensions(myImage.Width, myImage.Height, PictureBox1.Width, PictureBox1.Height);
Bitmap imgOutput = new Bitmap(myImage, fitImageSize.Width, fitImageSize.Height);
PictureBox1.Image = null;
PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
PictureBox1.Image = imgOutput;
}
private Size ScaledImageDimensions(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 (desiredImageWidth > desiredImageHeight)
{
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. */
int outputWidth = (int)(currentImageWidth * scaleImageMultiplier);
int outputHight = (int)(currentImageHeight * scaleImageMultiplier);
return new Size((currentImageWidth > outputWidth) ? outputWidth : currentImageWidth, (currentImageHeight > outputHight) ? outputHight : currentImageHeight);
}