将较小的图片变成较大的图片

时间:2018-07-16 18:23:56

标签: c# image bitmap

下面的程序代码可以工作,但是我没有得到想要的结果。

代码:

        using (Bitmap loadedImage = new Bitmap(alleDateien[z]))
        using (Bitmap bigImage = new Bitmap(loadedImage.Width + 3000, loadedImage.Height + 3000)) // 1a) Lese die Pixelanzahl aus
        {
            #region Init

            int bigImageWidth = bigImage.Width;
            int bigImageHeight = bigImage.Height;

            int loadedImageWidth = loadedImage.Width;
            int loadedImageHeight = loadedImage.Height;

            #endregion

            #region Das eine in das andere Bild

            Graphics g = Graphics.FromImage(bigImage);

            int tempX = bigImage.Width / 2 - loadedImage.Width / 2;
            int tempY = bigImage.Height / 2 - loadedImage.Height / 2;

            g.DrawImage(loadedImage, /* x-Position: */tempX, /* y-Position: */tempY);

            #endregion

            #region Speichern

            bigImage.Save(Path.Combine(deleteNewPath, Dateiname(Path.GetFileName(alleDateien[z]), "", beliebigVieleDateien: false, ohneDatum: true)));

            #endregion
        }

但是这段代码是关于什么的?

我想将较小的图像放到较大的图像中(实际上是在其周围构建框架)

结果如何?

有时候精度,分辨率,清晰度或任何您想称呼的东西...在合并之前和合并之后图像的质量都丢失了,如果没有合并,我看不到我会看到的每个细节(当我放大时) 。 像this。 如何解决呢?

应该是什么?

像原始照片一样。This

我该怎么办?

1 个答案:

答案 0 :(得分:0)

Bitmap(loadedImage.Width + 3000, loadedImage.Height + 3000)

新图像太大,超过3000x3000像素。它不适合您的桌面屏幕,因此位图查看器必须将其缩小。它没有丢失任何分辨率。缩小图像尺寸,如下所示:

Bitmap old_image = new Bitmap("old.bmp");

//border thickneess
int border_thickness = 10;

Bitmap new_image = new Bitmap(
        old_image.Width + border_thickness, old_image.Height + border_thickness);

//draw on new image
Graphics g = Graphics.FromImage(new_image);

//find the x/y position to put the old image in center
int x = (new_image.Width - old_image.Width) / 2;
int y = (new_image.Height - old_image.Height) / 2;

//draw a border
SolidBrush brush = new SolidBrush(Color.Red);
Rectangle rect = new Rectangle(0, 0, new_image.Width, new_image.Height);
g.FillRectangle(brush, rect);

//draw old image in to new image
g.DrawImage(old_image, x, y);

//save
new_image.Save("new.bmp");