c# - Parameter is not valid while try to save image -
i have method in fileutility
class save images.
public static void saveimage(system.io.stream file, string savepath, size size, bool enforceratio, bool testreverse = false) { var image = image.fromstream(file); var imageencoders = imagecodecinfo.getimageencoders(); int enc = path.getextension(savepath).tolower().contains("png") ? 4 : 1; encoderparameters encoderparameters = new encoderparameters(1); encoderparameters.param[0] = new encoderparameter(system.drawing.imaging.encoder.quality, 90l); size s = new size(size.width, size.height); if (testreverse) { if (image.width <= image.height) { if (size.width <= size.height) { s.width = size.width; s.height = size.height; } else { s.width = size.height; s.height = size.width; } } else { if (size.width <= size.height) { s.width = size.height; s.height = size.width; } else { s.width = size.width; s.height = size.height; } } } var canvaswidth = s.width; var canvasheight = s.height; var newimagewidth = s.width; var newimageheight = s.height; var xposition = 0; var yposition = 0; if (enforceratio) { var ratiox = s.width / (double)image.width; var ratioy = s.height / (double)image.height; var ratio = ratiox < ratioy ? ratiox : ratioy; newimageheight = (int)(image.height * ratio); newimagewidth = (int)(image.width * ratio); } var thumbnail = new bitmap(canvaswidth, canvasheight); var graphic = graphics.fromimage(thumbnail); if (enforceratio) { graphic.clear(color.white); } graphic.interpolationmode = interpolationmode.highqualitybicubic; graphic.smoothingmode = smoothingmode.highquality; graphic.pixeloffsetmode = pixeloffsetmode.highquality; graphic.compositingquality = compositingquality.highquality; graphic.drawimage(image, xposition, yposition, newimagewidth, newimageheight); thumbnail.save(savepath, imageencoders[enc], encoderparameters); }
it work well. images files (not every file)
argument exception : parameter not valid
in first line var image = image.fromstream(file);
i call method this:
fileutility.saveimage(add2.iconsquare.inputstream, server.mappath(program.iconsquare), new system.drawing.size { width = 250, height = 250 }, false);
that add2.iconsquare httppostedfilebase
public httppostedfilebase iconsquare { get; set; }
the thing add2 come action tempdata.
var add2 = tempdata["create_step2"] addstep2;
i mean take files user in action create_step2 , save them in tempdata try save them in action create_step3. can tell me doing wrong?
Comments
Post a Comment