GetBitmapFromCollection problem

I have a problem trying to get a copy of a diagram onto the clipboard because there seems to be no way to ask for a bitmap via GetBitmapFromCollection with a different System.Drawing.Imaging.PixelFormat, and the bitmap being returned in larger cases exceeds 60MB!! I assume this is a 32bpp image, completely unecessary for our needs, and obviously too big for the clipboard or anything useful.



I tried to convert it down to a 8bpp Bitmap in the following way:



Bitmap b32 = this.goView.GetBitmapFromCollection((goView.Document);

Bitmap b8 = b32.Clone(new Rectangle(0,0,b32.Width, b32.Height), System.Drawing.Imaging.PixelFormat.Format8bppIndexed);



But this just generates an “Out of memory” exception.



Any suggestions?

Here’s a simplified method that does the basic operations, including displaying view objects, for a subclass of GoView. You can change how the Bitmap is created.
public virtual Bitmap GetBitmap() {
<SPAN =527123110-27052004> Rectangle dispRect = this.DisplayRectangle;
<SPAN =527123110-27052004> Bitmap buf = new Bitmap(dispRect.Width, dispRect.Height);
<SPAN =527123110-27052004> Graphics gbuf = Graphics.FromImage(buf);
<SPAN =527123110-27052004> gbuf.ScaleTransform(this.DocScale, this.DocScale);
<SPAN =527123110-27052004> PointF viewLoc = this.DocPosition;
<SPAN =527123110-27052004> gbuf.TranslateTransform(-viewLoc.X, -viewLoc.Y);
<SPAN =527123110-27052004> PaintView(gbuf, new RectangleF(viewLoc, ConvertViewToDoc(dispRect.Size)));
<SPAN =527123110-27052004> gbuf.Dispose();
<SPAN =527123110-27052004> return buf;
}

Ok thanks, that gives me some new things to try :)

This helps a little but perhaps you could save me a LOT of time by giving me something i can override the GetBitmapFromCollection() method with? This GetBitmap function misses most of what I need, which is a bitmap of the entire document on a white background.





Call GoDocument.ComputeBounds to figure out how big to make the bitmap.
Replace the call to PaintView with calls to PaintBackgroundDecoration and PaintObjects. You might also want to set the Graphics’ SmoothingMode, TextRenderingHint, and InterpolationMode. The only thing PaintView does extra is a call to PaintPaperColor, which I guess you don’t want to do.

Now I get an offset bitmap that does not hold the whole drawing. I have no idea what I´m doing wrong here, this all worked perfectly with GetBitmapFromCollection() but now I´m stuck and worse off than before :(



public virtual Bitmap GetBitmap()

{

RectangleF rect=this.Document.ComputeBounds();

Rectangle rect2 = ConvertDocToView(rect);

Bitmap buf = new Bitmap(rect2.Width, rect2.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);



Graphics gbuf = Graphics.FromImage(buf);



//gbuf.ScaleTransform(this.Doc Scale, this.DocScale);



PointF viewLoc = new PointF(0f,0f);



//gbuf.TranslateTransform(-vie wLoc.X, -viewLoc.Y);



this.DocPosition=viewLoc;

this.PaintPaperColor(gbuf, new RectangleF(viewLoc, this.DocumentSize));

this.PaintObjects(true, false, gbuf, new RectangleF(viewLoc, this.DocumentSize));



gbuf.Dispose();



return buf;



}

This is defintely not what I want…

I altered the transform settings, and now I´m NEARLY back to my starting point, but the paper painting seems to be messed up… :(
RectangleF rect=this.Document.ComputeBounds();

Rectangle rect2 = ConvertDocToView(rect);
Bitmap buf = new Bitmap(rect2.Width, rect2.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);

Graphics gbuf = Graphics.FromImage(buf);

//gbuf.ScaleTransform(this.DocScale, this.DocScale);
PointF viewLoc = rect.Location;
gbuf.TranslateTransform(-viewLoc.X, -viewLoc.Y);
//this.DocPosition=viewLoc;
this.PaintPaperColor(gbuf, new RectangleF(viewLoc, this.DocumentSize));
this.PaintObjects(true, false, gbuf, new RectangleF(viewLoc, this.DocumentSize));
gbuf.Dispose();
return buf;

This would all be really easy if I had some code for GetBitmapFromCollection. I don´t want to waste any more time reinventing the wheel. I can´t get anywhere near what I orginally had.

Here’s what I would try. This does not depend on being a method of a GoView, so you don’t have to subclass GoView.
public Bitmap GetBitmap(GoDocument doc) {
RectangleF docBounds = doc.ComputeBounds();
Size bmsize = new Size((int)docBounds.Width, (int)docBounds.Height);
float sc = Math.Min(2048f/bmsize.Width, 2048f/bmsize.Height);
if (sc > 1) sc = 1;
bmsize.Width=(int)(scbmsize.Width);
bmsize.Height=(int)(sc
bmsize.Height);
// you can customize how the Bitmap is created here:
Bitmap buf = new Bitmap(bmsize.Width, bmsize.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics gbuf = Graphics.FromImage(buf);
GoView view = new GoView();
Color color = doc.PaperColor;
if (color == Color.Empty)
color = view.BackColor;
gbuf.FillRectangle(new SolidBrush(color), 0, 0, bmsize.Width, bmsize.Height);
PointF viewLoc = docBounds.Location;
gbuf.ScaleTransform(sc, sc);
gbuf.TranslateTransform(-viewLoc.X, -viewLoc.Y);
foreach (GoObject obj in doc) {
if (!obj.CanView())
continue;
obj.Paint(gbuf, view);
}
gbuf.Dispose();
return buf;
}