The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Foto/Grafik-Manipulationen einfach und schnell - Beispiel Sepia

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
-

Posts: 1524
Nickname: nitronic
Registered: Jul, 2006

Norbert Eder works as a software architect.
Foto/Grafik-Manipulationen einfach und schnell - Beispiel Sepia Posted: Jan 3, 2008 12:46 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by -.
Original Post: Foto/Grafik-Manipulationen einfach und schnell - Beispiel Sepia
Feed Title: Norbert Eder - Living .NET
Feed URL: http://feeds.feedburner.com/NorbertEder-Livingnet
Feed Description: Copyright (c)2005, 2006 by Norbert Eder
Latest .NET Buzz Posts
Latest .NET Buzz Posts by -
Latest Posts From Norbert Eder - Living .NET

Advertisement
In vielen Anwendungen werden Grafiken jeglicher Art angezeigt. Und manches Mal, wollen diese auch manipuliert werden. Die Gründe sind hier manigfaltig. In meinem Fall mussten Fotos sepia-färbig dargestellt werden. Mit Hilfe der Klasse ColorMatrix aus dem Namespace System.Drawing.Imaging (System.Drawing.dll) läßt sich dies in wenigen Sekunden bewerkstelligen.

Doch was genau macht die Klasse ColorMatrix? Hier die treffende Beschreibung aus der MSDN:

Defines a 5 x 5 matrix that contains the coordinates for the RGBA space. Several methods of the ImageAttributes class adjust image colors by using a color matrix.

Anmerkung: A ist der Alpha-Kanal, welcher die Transparenz einer Farbe beschreibt.

Sepia selbst ist nichts anderes, als die Darstellung einer Grafik/eines Bildes in Brauntönen, anstatt in Grau-Schattierungen.

Nachfolgend wird nun die Matrix für unsere Sepia-Darstellung definiert (Nachschlagen, Grafiker fragen, alles ist erlaubt):
private ColorMatrix GetSepiaMatrix()
{
    return new ColorMatrix(new float[][]
    {
        new float[] {0.393f, 0.349f, 0.272f, 0, 0},
        new float[] {0.769f, 0.686f, 0.534f, 0, 0},
        new float[] {0.189f, 0.168f, 0.131f, 0, 0},
        new float[] {     0,      0,      0, 1, 0},
        new float[] {     0,      0,      0, 0, 1}
    });
}

Nun muss die Matrix nur mehr verwendet werden. Hier der Code aus dem Button-Click-Event meiner kleinen Demo-Anwendung:
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "JPEG-Files:*.jpg";
ofd.DefaultExt = ".jpg";
ofd.Title = "Open JPG File";

if (ofd.ShowDialog() == DialogResult.OK)
{
    if (OriginalPicture.Image != null)
        OriginalPicture.Image.Dispose();
    if (SepiaPicture.Image != null)
        SepiaPicture.Image.Dispose();

    Image img1 = Image.FromFile(ofd.FileName);
    Image img2 = Image.FromFile(ofd.FileName);

    OriginalPicture.Image = img1;

    ImageAttributes imageAttrs = new ImageAttributes();
    imageAttrs.SetColorMatrix(GetSepiaMatrix());
    using (Graphics g = Graphics.FromImage(img2))
    {
        g.DrawImage(
            img2, 
            new Rectangle(0, 0, img2.Width, img2.Height),
            0, 0, img2.Width, img2.Height, 
            GraphicsUnit.Pixel, imageAttrs);
    }

    SepiaPicture.Image = img2;
}

Anmerkungen: OriginalPicture und SepiaPicture sind beide vom Typ PictureBox.

Und so kann ein mögliches Ergebnis ausssehen:



PS: Dieses Foto wurde übrigens 2005 von mir in Venedig aufgenommen. Schon eine recht nette Stadt muss ich sagen, wenn es auch ständig nach Fisch riecht ;-)

Read: Foto/Grafik-Manipulationen einfach und schnell - Beispiel Sepia

Topic: Axinom goes CES 2008 in Las Vegas Previous Topic   Next Topic Topic: WPF Teil 3: Das Softwaredesign will gut überlegt sein

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use