c# - Bitmap graphics: when saving on disk no drawed strings - works when memorystream -


i creating png picture, using bitmap object, using drawing.graphics . create bitmap, insert background image , draw strings.

now, when save image on disk, files not have strings!

i doing in asp.net mvc, controllers signature:

    [acceptverbs(httpverbs.get)]     public string getnewsletterpicture(string headline, string tagline) 

when don't save image on disk , instead returns filestreamresult memorystream, image looks perfectly.

so there problem when save image disk, strings "forgotten" somehow.

any ideas?

my code:

colorconverter converter = new colorconverter();         color textcolor = (color)converter.convertfromstring("#ff58595b");         int width = 598;         int height = 77;         int offset = 40;         int shadowoffset = 1;          var bmp = new bitmap(width, height);         using (graphics g = graphics.fromimage(bmp))         {             g.clear(color.lightgray);             image backgroundimg = new bitmap(server.mappath("~/static/images/bgimg.png"));             g.drawimage(backgroundimg,0,0);              stringformat sf= new stringformat();             sf.alignment = stringalignment.center;              var rectangletop = new rectanglef(0, 0, width, height);             var rectangletopshadowhack = new rectanglef(shadowoffset, shadowoffset, width + shadowoffset, height + shadowoffset);             g.textrenderinghint = textrenderinghint.antialiasgridfit;              // show headline , center             if (!string.isnullorempty(tagline))             {                 var rectanglebottomshadowhack = new rectanglef(shadowoffset, offset + shadowoffset, width + shadowoffset, height - offset + shadowoffset);                 var rectanglebottom = new rectanglef(0, offset, width, height - offset);                  g.drawstring(tagline, new font("verdana", 18), new solidbrush(color.white), rectanglebottomshadowhack, sf);                 g.drawstring(tagline, new font("verdana", 18), new solidbrush(textcolor), rectanglebottom, sf);             }             else             {                 sf.linealignment = stringalignment.center;             }             g.drawstring(headline, getfont("sentinel-bold", 28, fontstyle.bold), new solidbrush(color.white), rectangletopshadowhack, sf);             g.drawstring(headline, getfont("sentinel-bold", 28, fontstyle.bold), new solidbrush(textcolor), rectangletop, sf);              g.save();              var filename = guid.newguid().tostring() + ".png";             var path = server.mappath("~/static/previews/" + filename);             bmp.save(path, imageformat.png);              return filename; 

if in doubt, g.drawstring not being saved on picture.

new atttempt (still not working):

[acceptverbs(httpverbs.get)]         public string getnewsletterpicture(string headline, string tagline)         {             colorconverter converter = new colorconverter();             color textcolor = (color)converter.convertfromstring("#ff58595b");             int width = 598;             int height = 77;             int offset = 40;             int shadowoffset = 1;              var bmp = new bitmap(width, height);             using (graphics g = graphics.fromimage(bmp))             {                 g.clear(color.lightgray);                 image backgroundimg = new bitmap(server.mappath("~/static/images/bgimg.png"));                 g.drawimage(backgroundimg,0,0);                  stringformat sf= new stringformat();                 sf.alignment = stringalignment.center;                  var rectangletop = new rectanglef(0, 0, width, height);                 var rectangletopshadowhack = new rectanglef(shadowoffset, shadowoffset, width + shadowoffset, height + shadowoffset);                 g.textrenderinghint = textrenderinghint.antialiasgridfit;                  // show headline , center                 if (!string.isnullorempty(tagline))                 {                     var rectanglebottomshadowhack = new rectanglef(shadowoffset, offset + shadowoffset, width + shadowoffset, height - offset + shadowoffset);                     var rectanglebottom = new rectanglef(0, offset, width, height - offset);                      g.drawstring(tagline, new font("verdana", 18), new solidbrush(color.white), rectanglebottomshadowhack, sf);                     g.drawstring(tagline, new font("verdana", 18), new solidbrush(textcolor), rectanglebottom, sf);                 }                 else                 {                     sf.linealignment = stringalignment.center;                 }                 g.drawstring(headline, getfont("sentinel-bold", 28, fontstyle.bold), new solidbrush(color.white), rectangletopshadowhack, sf);                 g.drawstring(headline, getfont("sentinel-bold", 28, fontstyle.bold), new solidbrush(textcolor), rectangletop, sf);                  g.flush(flushintention.sync);             }              var filename = guid.newguid().tostring() + ".png";             var path = server.mappath("~/static/previews/" + filename);             bmp.save(path, imageformat.png);              return filename;               //memorystream stm = new memorystream();             //bmp.save(stm,system.drawing.imaging.imageformat.png);             //stm.position = 0;              //return new filestreamresult(stm, "image/png");         } 

i can't tell sure, looks might confusing g.save() g.flush().

you need call g.flush(flushintention.sync) instead of g.save(). should call bmp.save() outside of using block:

var bmp = new bitmap(width, height); using (graphics g = graphics.fromimage(bmp)) {   //...   g.flush(flushintention.sync); }  var filename = guid.newguid().tostring() + ".png"; var path = server.mappath("~/static/previews/" + filename);  bmp.save(path, imageformat.png) 

save() used save current graphics state can modify , restore later.:

graphicsstate oldstate = g.save();  // make changes graphics state...  g.restore(oldstate); 

flush() on other hand, used force graphics object complete pending operations. passing flushintention.sync parameter, flush() won't return until flushing complete.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -