使用GDI和.NET应用转换和组合位图

时间:2012-07-28 08:54:04

标签: c#

我正在创建一个加载2个图像的方法,对它们应用一些转换(缩放/旋转),然后使用.NET Graphics类在另一个图像上的某个点上绘制它们。

我目前正在尝试的流程如下:

Load bmp1
Load bmp2
Create graphics object from bmp1
Apply tranformations
Save
Repeat process for bmp2
Instantiate new merged bmp obj
Create graphics object from merged bmp
Draw bmp1 & bmp2 on merged bmp
Save

我正尝试使用以下代码执行此操作:

// load existing bmps
var bmp1 = new Bitmap(Path.Combine(_staticIconsPath, staticIcon.Filename));
var bmp2 = new Bitmap(Path.Combine(_movableIconsPath, movableIcon.Filename));

// apply transforms to bmp1
var canvas1 = Graphics.FromImage(bmp1);
canvas1.ScaleTransform(0.5f, 0.5f);
canvas1.RotateTransform(45.0f);
canvas1.Save();

// apply transforms to bmp2
var canvas2 = Graphics.FromImage(bmp2);
canvas2.ScaleTransform(0.5f, 0.5f);
canvas2.RotateTransform(45.0f);
canvas2.Save();

// create final merged bmp
var mergedBmp = new Bitmap(590, 590);
mergedBmp.SetResolution(bmp1.HorizontalResolution, bmp1.VerticalResolution);

// draw transformed images on to final bmp
var mergedCanvas = Graphics.FromImage(mergedBmp);
mergedCanvas.DrawImage(bmp1, 0, 0);
mergedCanvas.DrawImage(bmp2, 0, 0);

mergedCanvas.Save();

然而,最终结果只是加载的两个原始位图,两者都按预期绘制在彼此之上,但没有应用变换。

我之前从未使用过GDI或进行任何图像处理,所以我认为我误解了必须遵循的过程的基本内容。任何人都可以帮助我并指出我正确的方向吗?

2 个答案:

答案 0 :(得分:2)

你必须在每个bmp文件中保存名为canevas的转换。 您可以使用这些bmp来绘制合并的文件。但是不要设置相同的位置 当drawingImage,因为在同一位置的两个图像,你只会看到一个,最后一个。

            // load existing bmps 
        var bmp1 = new Bitmap("test2.bmp");
        var bmp2 = new Bitmap("test2.bmp");

        // apply transforms to bmp1 
        var canvas1 = Graphics.FromImage(bmp1);
        canvas1.ScaleTransform(0.5f, 0.5f);
        canvas1.RotateTransform(45.0f);
        canvas1.Save();
        canvas1.DrawImage(bmp1,100, 0);
        bmp1.Save("test1res.bmp");
        var bmpres1 = new Bitmap("test1res.bmp");

        // apply transforms to bmp2
        var resbmp2 = new Bitmap(1000, 1000);
        var canvas2 = Graphics.FromImage(bmp2);
        canvas2.ScaleTransform(0.5f, 0.5f);
        canvas2.RotateTransform(45.0f);
        canvas2.Save();
        canvas2.DrawImage(bmp2, 100, 0);
        bmp2.Save("test2res.bmp");
        var bmpres2 = new Bitmap("test2res.bmp");

        // create final merged bmp 
        var mergedBmp = new Bitmap(1000, 1000);
        mergedBmp.SetResolution(bmp1.HorizontalResolution, bmp1.VerticalResolution);

        // draw transformed images on to final bmp 
        var mergedCanvas = Graphics.FromImage(mergedBmp);
        mergedCanvas.DrawImage(bmpres1, 0, 0);
        mergedCanvas.DrawImage(bmpres2, 500, 0);

        mergedCanvas.Save();
        Graphics graph = mergedCanvas;
       // Bitmap bmpPicture = new Bitmap("test2.bmp");
        graph.DrawImage(mergedBmp, 0, 0);

        mergedBmp.Save("res.bmp");

答案 1 :(得分:1)

是的,你做错了。除非您实际使用它绘制,否则Graphics对象修改无效。你没有,你只用mergedCanvas绘制。因此,重新订购代码,类似于:

var mergedBmp = new Bitmap(590, 590);
using (var bmp1 = new Bitmap(Path.Combine(_staticIconsPath, staticIcon.Filename)))
using (var bmp2 = new Bitmap(Path.Combine(_movableIconsPath, movableIcon.Filename)))
using (var mergedCanvas = Graphics.FromImage(mergedBmp)) {
    mergedCanvas.Save();
    mergedCanvas.ScaleTransform(0.5f, 0.5f);
    mergedCanvas.DrawImage(bmp1, 0, 0);
    mergedCanvas.Restore();
    mergedCanvas.ScaleTransform(-0.5f, -0.5f);
    mergedCanvas.DrawImage(bmp2, 0, 0);
}

请勿跳过那些使用语句的 btw,这对位图非常重要。