xaml to tiff RRS feed

  • Pregunta

  • Hi, i have the next code to export xaml that I have load in a canvas to a tiff

    visual =

    new DrawingVisual();
    context = visual.RenderOpen();
    elementBrush =
    new VisualBrush(element);



    //Obtener la imgen
    //context.DrawRectangle(elementBrush, null, new Rect(0, 0, tam.Width, tam.Height));
    double dpiXd = Convert.ToInt32(dpiX);
    double dpiYd = Convert.ToInt32(dpiY);
    double convertX = 96.0 / dpiXd;
    double convertY = 96.0 / dpiYd;
    int ancho = Convert.ToInt32(tam.Width * convertX);
    int alto = Convert.ToInt32(tam.Height * convertX);
    null, new Rect(0, 0, ancho, alto));
    res =
    new RenderTargetBitmap(tam.Width, tam.Height, 300, 300, PixelFormats.Pbgra32);

    the problem is that the quality of the tiff result is lower than source images that compose the xaml.
    The source images from the xaml are in CMYK, but if i put PixelFormats.cmyk32 or other format give me an error, How can i do it?

    thanks, Oscar

    jueves, 4 de febrero de 2010 12:48


  • Hello Racsus.

    Only you can convert between these formats, encoding, even so, for this you must convert it first with 'FormatConvertedBitmap' something like the following example:

    Image myImage = new Image();
    FormattedText text = new FormattedText("Prueba de texto",
            new System.Globalization.CultureInfo("es-es"),
            new Typeface(this.FontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch()),
    DrawingVisual drawingVisual = new DrawingVisual();
    DrawingContext drawingContext = drawingVisual.RenderOpen();
    drawingContext.DrawText(text, new Point(2, 2));
    RenderTargetBitmap bmp = new RenderTargetBitmap(180, 180, 96, 96, PixelFormats.Pbgra32);
    FormatConvertedBitmap newFormatedBitmapSource = new FormatConvertedBitmap();
    // BitmapSource objects like FormatConvertedBitmap can only have their properties
    // changed within a BeginInit/EndInit block.
    // Use the BitmapSource object defined above as the source for this new 
    // BitmapSource (chain the BitmapSource objects together).
    newFormatedBitmapSource.Source = bmp;
    // Set the new format
    newFormatedBitmapSource.DestinationFormat = PixelFormats.Prgba128Float;
    //set image source
    myImage.Source = newFormatedBitmapSource;

    Even so, this will not solve the problem because whenever rescale an image, you lose quality.To avoid loss of quality must save the image with the same resolution and size, in case you want another size, you should use some filters.

    You're using a class for treatment of vector elements, if vectors are going to create this class is ideal, but for working with images should make use of specific classes for pictures.

    David González
    jueves, 4 de febrero de 2010 15:46