0

我正在创建一个加载 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 或做过任何图像处理,所以我认为我误解了必须遵循的过程的基本内容。任何人都可以帮助我并指出我正确的方向吗?

4

2 回答 2

2

您必须将名为 canevas 的转换保存在每个转换的 bmp 文件中。在你可以使用那些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");
于 2012-07-28T10:55:03.150 回答
1

是的,你做错了。除非您实际使用它进行绘图,否则 Graphics 对象的修改不会产生任何影响。你不这样做,你只用 mergeCanvas 绘制。所以重新排序你的代码,类似于这样:

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);
}

顺便说一句,不要跳过那些using语句,这在位图上非常重要。

于 2012-07-28T12:16:37.647 回答