c# - Problem with using server-side clipboard in ASPX webapp -
i've got reporting application runs server-side reads stored bmp database (as byte[]), converts image, , places excel spreadsheet forms base report (this report delivered client download.) i'm trying use server-side clipboard handle 'pasting' of image specific range in worksheet. here's code snippet -
system.drawing.image image; bitmap bm; graphics g; excel.range range; memorystream ms = new memorystream(graphrecs.elementat(0).graph, 0, graphrecs.elementat(0).graph.length); ms.write(graphrecs.elementat(0).graph, 0, graphrecs.elementat(0).graph.length); image = system.drawing.image.fromstream(ms, true); bm = new bitmap(413, 130); g = graphics.fromimage(bm); g.interpolationmode = system.drawing.drawing2d.interpolationmode.highqualitybicubic; g.drawimage(image, 0, 1, 413, 130); clipboard.setdataobject(bm, false, 4, 250); range = ws.get_range(cblkptr[6, 2], cblkptr[6, 2]); ws.paste(range, bm); clipboard.clear();
running in debug mode under vs2008 seems work fine - image converted, added clipboard, , pasted specified range no problems. after publish webapp iis server, fails on 'clipboard.setdataobject' statement following exception -
requested clipboard operation did not succeed. @ system.windows.forms.clipboard.throwiffailed(int32 hr) @ system.windows.forms.clipboard.setdataobject(object data, boolean copy, int32 retrytimes, int32 retrydelay) @ reportgenerate.buildpvsclinicsections(worksheet ws, object j, patientrecord p, string patientstatus, string programtype)
i under assumption error has not being in singlethreadapartment. i've added 'aspcompat=true' directive aspx page no change (didn't think aspcompat more asp aspx). since can't add [stathread] 'main' (that iis), i'm @ loss on how proceed. i'm open changing approach i'm using in adding image spreadsheet, long can explicitly specify (via range) place it. using shape.addpicture instance doesn't allow me this.
any ideas?
thanks.
update
i've updated code snippet start second thread correct apartmentstate -
range = ws.get_range(cblkptr[6, 2], cblkptr[6, 2]); clipboardmodel cbm = new clipboardmodel(bm, range, ws); system.threading.thread cbthread = new system.threading.thread(new system.threading.parameterizedthreadstart(doclipboardstuff)); cbthread.setapartmentstate(system.threading.apartmentstate.sta); cbthread.start(cbm); cbthread.join();
the 'doclipboardstuff' method looks -
[stathread] protected void doclipboardstuff(object o) { try { clipboardmodel cbm = (clipboardmodel)o; clipboard.setdataobject(cbm.bm, false, 4, 250); cbm.ws.paste(cbm.range, cbm.bm); clipboard.clear(); } catch (exception e) { streamwriter sw = new streamwriter(@"c:\myopia\log.txt"); sw.writeline(e.message); sw.writeline(e.stacktrace); sw.flush(); sw.close(); throw e; } }
i'm getting exact same error before, in method. i'm beginning suspect it's not apartmentstate, rather lack of 'ui'. don't know if normal win32 interface better, that's next approach (unless else has more, .net'ish solution.)
update #2
while haven't been able resolve issue iis 6 , clipboard, i've managed work around problem writing reconstructed bmp temp file, using shapes.addpicture place need -
g.drawimage(image, 0, 1, 400, 75); bm.save(@"c:\myopia\temp.bmp"); excel.shape xlshape = ws.shapes.item("rectangle 2"); float left = xlshape.left; float top = convert.tosingle(ws.get_range("a1", cblkptr[5, 2]).height); float width = xlshape.width; float height = xlshape.height; xlshape = ws.shapes.addpicture(@"c:\myopia\temp.bmp", microsoft.office.core.msotristate.msofalse, microsoft.office.core.msotristate.msoctrue, left, top, width, height);
not ideal solution, 1 works now. issue approach seem have loss of resolution between reconstructing bmp byte[], saving temp.bmp file, , adding in - bmp looks 'fuzzy'. may have less 'lossy' format use.
if sta indeed problem try performing operation in new thread set sta before start shown in skeet(tm)'s answer question:
in .net, how set stathread when i'm running form in additional thread?
somewhere deep in of mind though little voice suggesting clipboard might available apps ui (the dev web server does example)... hope i'm wrong though!
Comments
Post a Comment