从WCF Web服务中的对象数组创建图像数组

时间:2015-11-03 11:14:29

标签: c# arrays wcf

我有一个像这样的2D椅子阵列:chairs[][]这里也是我的椅子类:

public class Chair
{
    public int State;
    public int number;
}

班级中的每个对象代表剧院中的座位(状态表示座位状态; 0表示空,1表示保留,数字表示座位号)。

我们假设我有这些值:

    0   0   0   1
    1   1   1   0
    0   1   0   1

我想创建这样的图像数组:

enter image description here

最后将其转换为字节数组并将其作为Web服务输出返回。 长话短说我想将chair[][]的数组转换为图像。我的网络服务输入为chairs[][],输出为byte[]

这可能吗?

更新

这里我在windows桌面应用程序中创建了一些东西。我只需要在wcf中创建它。我现在知道如何在wcf !!!

中创建PictureboxLabal
{
    AutoScroll = true;
    int x = 10, y = 10;

    Chair[][] chairs = new Chair[3][];
    chairs[0] = new Chair[4] { new Chair { number = 1, State = 0 }, new Chair { number = 2, State = 0 }, new Chair { number = 3, State = 0 }, new Chair { number = 4, State = 1 } };
    chairs[1] = new Chair[4] { new Chair { number = 5, State = 1 }, new Chair { number = 6, State = 1 }, new Chair { number = 7, State = 1 }, new Chair { number = 8, State = 0 } };
    chairs[2] = new Chair[4] { new Chair { number = 9, State = 0 }, new Chair { number = 10, State = 1 }, new Chair { number = 11, State = 0 }, new Chair { number = 12, State = 1 } };

    Label[][] label = new Label[chairs.Length][];
    PictureBox[][] picturebox = new PictureBox[chairs.Length][];

    for (int i = 0; i < chairs.Length; i++)
    {
        label[i] = new Label[chairs[i].Length];
        picturebox[i] = new PictureBox[chairs[i].Length];


        for (int j = 0; j < chairs[i].Length; j++)
        {
            label[i][j] = new Label();
            picturebox[i][j] = new PictureBox();

            Controls.Add(label[i][j]);
            Controls.Add(picturebox[i][j]);

            label[i][j].Location = new Point(x, y + 50);
            picturebox[i][j].Location = new Point(x, y);

            label[i][j].Size = new Size(100, 20);
            picturebox[i][j].Size = new Size(50, 50);

            label[i][j].Text = i + "*" + j + "(" + chairs[i][j].number + ")";
            if (chairs[i][j].State == 0) picturebox[i][j].Image = Image.FromFile(@"C:\Users\Admin\Desktop\1\1.png");
            else picturebox[i][j].Image = Image.FromFile(@"C:\Users\Admin\Desktop\1\2.png");

            x = x + 100;
        }
        x = 10;
        y = y + 100;
    }

2 个答案:

答案 0 :(得分:1)

首先 - https://msdn.microsoft.com/en-us/library/vstudio/ms734712(v=vs.100).aspx

然后

// Service side
[ServiceContract(Name = "chairs-image-generator")]
public interface IChairsImageArrayGenerator
{
    // insted byte[] paste desirable type of image
    [OperationContract(Name = "generate-images")]
    IEnumerable<byte[]> GenerateImage(IEnumerable<byte[][]> chairs);
}

// Client side
[ServiceContract(Name = "chairs-image-generator")]
public interface IChairsImageArrayGeneratorClient
{
    [OperationContract(Name = "generate-images")]
    IEnumerable<byte[]> GenerateImages(IEnumerable<byte[][]> chairs);
}

public class ChairsImageGenerator : IChairsImageArrayGenerator
{
    public IEnumerable<byte[]> GenerateImages(IEnumerable<byte[][]> chairs)
    {
        // put here your realization 
    }
}

答案 1 :(得分:1)

string firstText = "Hello";
string secondText = "World";

PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);

string imageFilePath = @"path\picture.bmp"
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file

using(Graphics graphics = Graphics.FromImage(bitmap))
{
   using (Font arialFont =  new Font("Arial", 10))
   {
       graphics.DrawString(firstText, arialFont,Brushes.Blue,firstLocation);
       graphics.DrawString(secondText,arialFont,Brushes.Red,secondLocation);
    }
}

bitmap.Save(imageFilePath);//save the image