Saving Image problem( background )

Walter,



Thank you for your quick response.

I have another problem in saving image of diagram.

I can’t change the background of image. It could be only black.

I forced the diagrampanel background assigned to Colors.White.(A255,R255,G255,B255)

After setting background, myModeler.Panel.Background’s color values are A0, R255, G255, B255. I don’t know the reason. ?



my codes here…

<>

private void MenuItem_MouseLeftButtonDown_5(object sender, MouseButtonEventArgs e)

{

//save to image file

modelMeta.myModeler.Panel.Background = new SolidColorBrush(Colors.White);



bool currentGridState = false;



if (modelMeta.myModeler.GridVisible)

{

modelMeta.myModeler.GridVisible = false;

currentGridState = true;

}

//if image size over 2000x2000, scaling

Rect b = modelMeta.myModeler.Panel.DiagramBounds;

double w = b.Width;

double h = b.Height;

double s = 1.0;

if (w > 2000) s = 2000 / w;

if (h > 2000) s = Math.Min(s, 2000 / h);

w = s;

h = s;

var bmp = modelMeta.myModeler.Panel.MakeBitmap(new System.Windows.Size(w, h), 96, new Point(b.X, b.Y), s);



WriteableBitmap wBit = new WriteableBitmap(bmp);



modelMeta.myModeler.Panel.Background = new SolidColorBrush(Colors.Transparent);

modelMeta.myModeler.GridVisible = currentGridState;



SaveFileDialog sfd = new SaveFileDialog();

sfd.Filter = "pic file(
.jpg;
.bmp)|.jpg;.bmp";

bool? res = sfd.ShowDialog();



Stream str = sfd.OpenFile();



SaveToFile(wBit,str);



str.Close();

}





<

private static void SaveToFile(WriteableBitmap bitmap,Stream fs)

{

//Convert the Image to pass into FJCore

int width = bitmap.PixelWidth;

int height = bitmap.PixelHeight;

int bands = 3;



byte[][,] raster = new byte[bands][,];



for (int i = 0; i < bands; i++)

{

raster = new byte[width, height];

}



for (int row = 0; row < height; row++)

{

for (int column = 0; column < width; column++)

{

int pixel = bitmap.Pixels[width * row + column];

raster[0][column, row] = (byte)(pixel >> 16);

raster[1][column, row] = (byte)(pixel >> 8);

raster[2][column, row] = (byte)pixel;

}

}



ColorModel model = new ColorModel {colorspace = ColorSpace.RGB };



FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);



//Encode the Image as a JPEG

MemoryStream stream = new MemoryStream();

FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);



encoder.Encode();



//Move back to the start of the stream

stream.Seek(0, SeekOrigin.Begin);



//Get the Bytes and write them to the stream

byte[] binaryData = new Byte[stream.Length];

long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);



fs.Write(binaryData, 0, binaryData.Length );



}



here is the result jpg…

Try setting the Diagram.Background.

I already tried it. It just applied on viewport not whole diagram.

like this...

I can’t reproduce this problem.

Exactly what platform and version and which GoXam version are you using?

Hi walter

I am trying to save my diagram as a jpeg screenshot.
I am not able to save whole diagram.
The only visible part of the diagram is saving and rest behind the scrollviwer is clipped.
Also if i zoom out and get the whole diagram visible to user it saves the whole diagram but it still doesnot saves the background color of the other part which was behind the scrollviwer. It is coming in blackcolor.And rest diagram is coming in white color background.

Here are two images i am sharing with you.
This one is clipping the rest of the diagram and color is in black.

This one below is taken in case of zoomout the diagram.

now i have used the same technique you have mentioned in your Message%20Icon <span =“lgText”>Topic: Save image problem #2

Either tel me how to zoomout the image programmatically so that diagram fits into the panel and also how to set the background color that applies for whole diagram.

[QUOTE=goodhobak]I already tried it. It just applied on viewport not whole diagram.

like this…

[/quote]

Note i am using 1.1.3.3 version

You need to get the latest bug fix version of 1.1 from our evaluation/downloads page.

Use the new overloaded version of MakeBitmap that takes an Action as an argument, so that you can save the image after it has been fully rendered asynchronously. latest releases

Or better yet, upgrade to 1.2.6 if you can.

Hi walter

i have just upgraded the dll from 1.1.3 to 1.2.6.4
Now i am getting black image.No diagram is saving at all.the PNG contains only black color.
Note i have’nt change anything in the coding.besides the dll

Pls reply asap its been a long pending problem i am facing here.

So you are creating the bitmap in the Action that you pass as a last (new) argument?

FYI, saving images

Here is is code for saving the diagram.
Still having problem see the outputimage.

                      Rect bounds = diagram.myDiagram.Panel.DiagramBounds;
                        double w = bounds.Width;
                        double h = bounds.Height;
                        // calculate scale so maximum width or height is 200:
                        double s = 1;

diagram.myDiagram.Panel.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.LightGray);
System.Windows.Media.Imaging.BitmapSource bmp = diagram.myDiagram.Panel.MakeBitmap(new Size(w, h), 96, new System.Windows.Point(bounds.X, bounds.Y), s, BitmapConversion);
if (bData.Length != 0)
{
SaveFileDialog saveDlg = new SaveFileDialog
{
Filter = @“JPEG Files (.jpeg)|.jpeg”,
DefaultExt = @".jpeg"
};
if ((bool)saveDlg.ShowDialog())
{
using (Stream fs = saveDlg.OpenFile())
{
{
fs.Write(bData, 0, bData.Length);
}

                                }
                            }
                        }

private void BitmapConversion(BitmapSource Img)
{

            WriteableBitmap bitmap = new WriteableBitmap(Img);
            int width = bitmap.PixelWidth;
            int height = bitmap.PixelHeight;
            int bands = 3;
            byte[][,] raster = new byte[bands][,];

            try
            {
                for (int i = 0; i < bands; i++)
                {
                    raster<em> = new byte[width, height];
                }

                for (int row = 0; row < height; row++)
                {
                    for (int column = 0; column < width; column++)
                    {
                        long pixel = bitmap.Pixels[width * row + column];
                        raster[0][column, row] = (byte)(pixel >> 16);
                        raster[1][column, row] = (byte)(pixel >> 8);
                        raster[2][column, row] = (byte)pixel;
                    }
                }

                FluxJpeg.Core.ColorModel model = new FluxJpeg.Core.ColorModel { colorspace = FluxJpeg.Core.ColorSpace.RGB };
                FluxJpeg.Core.Image fimg = new FluxJpeg.Core.Image(model, raster);

                //Encode the Image as a JPEG
                using (MemoryStream mstream = new MemoryStream())
                {
                    FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(fimg, 100, mstream);
                    encoder.Encode();
                    //Back to the start
                    mstream.Seek(0, SeekOrigin.Begin);
                    //Get Bytes and write them to the stream
                    byte[] binaryData = new Byte[mstream.Length];
                    long bytesRead = mstream.Read(binaryData, 0, (int)mstream.Length);
                    bData = binaryData;
                  
                }
            }
            catch (Exception)
            {

            }    
                  
            
        }

It appears that you are writing out the “bData” to the file at a time which might occur before your “BitmapConversion” method has finished.

Try saving the file at the end of your “BitmapConversion” method.

Hi walter

I dont understand exactly what do you mean by this.
I am passing the byte[]dDAta only at the end of the function.
its just that the user needs to click twice to save the image.because at first time bdata is empty.
Note:- its only for the testing purpose only.

One more thing Walter
I need to save it as a png,so kindly let me know a way hot pass stream object toit.I mean through savefiledialog option how would i use it here???

I was just pointing out that the “bData” might be either null or only partly-filled, because the “BitmapConversion” method might not have executed yet.

You’ll have to search the web about how to write a PNG image file.

I seem to remember that Joe Stegner (Microsoft) published something like that.

Or you could use other image manipulation libraries for generating PNG files.

bdata has proper value.
saving png image i know but i asking here how to pass stream object in order to save the image using savefiledialog.??

I’ve never used SaveFileDialog in Silverlight, but it looks like your code is right. That is, to call dlg.OpenFile and then a Write method on that Stream.

HiWalter
I am still stuck in this problem.Also i have no idea whether this is a bug or not?

Have you looked at your “raster” array to make sure most of the pixels really are the background color that you want?

It’s a bit complicated because the “raster” is split up into bytes and because there need to be non-background-color pixels for the renderings of the nodes and links. But the last few pixels in each row and in each column ought to be the background color.

But in every case that I’ve tried, I have found that the WriteableBitmap had the correct background color pixels. And that showing an Image element using that bitmap in the app itself produced the desired looking image.

Actually, it isn’t clear to me why you are creating a new WriteableBitmap, since the Action argument will be a WriteableBitmap.