3

从文件对话框读取后,我想调整图片大小。我已经完成了以下代码。现在我想调整图片流的大小。我该怎么做?

Stream stream = (Stream)openFileDialog.File.OpenRead();
byte[] bytes = new byte[stream.Length];
4

3 回答 3

4

无需声明 a byte[],即可调整图像大小,只需使用

Image image = Image.FromFile(fileName);

检查this other answer以查看如何缩放图像

于 2013-01-14T10:23:04.110 回答
2

尝试这个

    public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
    {
        var ratioX = (double)maxWidth / image.Width;
        var ratioY = (double)maxHeight / image.Height;
        var ratio = Math.Min(ratioX, ratioY);

        var newWidth = (int)(image.Width * ratio);
        var newHeight = (int)(image.Height * ratio);

        var newImage = new Bitmap(newWidth, newHeight);
        Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
        return newImage;
    }

用法

        Image img = Image.FromStream(stream);
        Image thumb = ScaleImage(img);
        stream.Close();
        stream.Dispose();
        stream = new MemoryStream();
        thumb.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
于 2013-01-14T10:27:49.027 回答
1

我有一个图片框。我加载图像,调整大小并转换为字节,最后发送到 sqllite。也许对你来说可能是 hlepfıull 代码如下。

private static byte[] byteResim = null;

    private void btnResimEkle_Click(object sender, EventArgs e)
    {
        openFileDialog1.Title = "Resimdosyası seçiniz.";
        openFileDialog1.Filter = "Resim files (*.jpg)|*.jpg|Tüm dosyalar(*.*)|*.*";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {

            string resimYol = openFileDialog1.FileName; // File name of the image

            picResim.Image = Image.FromFile(resimYol);// picResim is name of picturebox
            picResim.Image = YenidenBoyutlandir(new Bitmap(picResim.Image)); //this method resizing the image
            Image UyeResim = picResim.Image;   // and this four block converting to image to byte
            MemoryStream ms = new MemoryStream();
            UyeResim.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byteResim = ms.ToArray();  // byteResim  variable format  Byte[]

        }
    }



    Image YenidenBoyutlandir(Image resim)// resizing image method 

    {
        Image yeniResim = new Bitmap(150, 156);
        using (Graphics abc = Graphics.FromImage((Bitmap)yeniResim))
        {
            abc.DrawImage(resim, new System.Drawing.Rectangle(0, 0, 150, 156));
        }
        return yeniResim;
    }
于 2017-07-09T13:49:15.180 回答