Frage

Ich schreibe eine CSS-Sprite-Engine in C #, jedoch bin ich ein paar Fragen. Ich schaffe das Master-Image, setzen Sie alle Eigenschaften, dass dann die sprites iterieren und den zum Master-Image ziehen. Allerdings, wenn ich gekommen, um das Master-Image zu speichern scheint es nur nur das leere Master-Image mit transparentem Hintergrund und keinem der sprites zu sein. Ich bin sehr verwirrt an, wohin ich gehe falsch.

Der Code Ich verwende ist:

    // Work out the width/height required
    int max_width = 0;
    int max_height = 0;

    foreach(SpriteInformation sprite in sprites) {
        if (max_width < (sprite.Left + greatest_width)) max_width = sprite.Left + greatest_width;
        if (max_height < (sprite.Top + greatest_height)) max_height = sprite.Top + greatest_height;
    }

    // Create new master bitmap
    Bitmap bitmap = new Bitmap(max_width,max_height,PixelFormat.Format32bppArgb);
    Graphics graphics = Graphics.FromImage(bitmap);

    // Set background color
    SolidBrush brush;

    if (cbxBackground.Checked) {
        if (txtColor.Text == "") {
            brush = new SolidBrush(Color.Black);
        } else {
            brush = new SolidBrush(pnlColor.BackColor);
        }
    } else {
        if (txtColor.Text == "") {
            brush = new SolidBrush(Color.White);
        } else {
            brush = new SolidBrush(pnlColor.BackColor);
        }
    }

    //graphics.FillRectangle(brush,0,0,bitmap.Width,bitmap.Height);
    bitmap.MakeTransparent(brush.Color);
    graphics.Clear(brush.Color);

    // Copy images into place
    ImageAttributes attr = new ImageAttributes();

    //attr.SetColorKey(brush.Color,brush.Color);

    foreach(SpriteInformation sprite in sprites) {
        Rectangle source = new Rectangle(0,0,sprite.Width,sprite.Height);
        Rectangle dest = new Rectangle(sprite.Left,sprite.Top,sprite.Width,sprite.Height);

        graphics.DrawImage(sprite.Sprite,dest,0,0,sprite.Width,sprite.Height,GraphicsUnit.Pixel,attr);
    }

    // Save image
    string format = ddlFormat.Items[ddlFormat.SelectedIndex].ToString();

    if (format == "PNG") {
        dlgSave.Filter = "PNG Images|*.png|All Files|*.*";
        dlgSave.DefaultExt = ",png";

        if (dlgSave.ShowDialog() == DialogResult.OK) {
            bitmap.Save(dlgSave.FileName,ImageFormat.Png);
        }
    } else if (format == "JPEG") {

    } else {

    }
War es hilfreich?

Lösung

Was ist sprite.Left und Top? Wenn sie nicht 0 sind, die ein Problem sein kann. Ich glaube, Sie haben dest und Quelle falsch herum?

http://msdn.microsoft.com/en-us/library /ms142045.aspx

eine einfachere DrawImage Variante versuchen, wenn Sie nicht bereits haben. z.B. DrawImage(sprite.Sprite,0,0)

Andere Tipps

Sie zeichnen auf „Grafik“, aber dann dann Sie speichern „Bitmap“? Ich bin mir nicht sicher, ob das Grafik intern mit Ihrem ursprünglichen „Bitmap“ Objekt funktioniert ...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top