Dear Expert i am getting an error while saving an image the code is as follows
ClsImageManager objImgManager = new ClsImageManager();
Bitmap ImageBitmap = objImgManager.GetBitmapFromBytes(ImageData);
Response.ContentType = "image/tiff";
ImageBitmap.Save(Response.OutputStream, ImageFormat.Tiff);
ImageBitmap.Dispose();
Response.End();
when i used Image.format.jpeg the code is working good but when i changes it to ImageFormat.Tiff then i am getting an error a generic error occurred in gdi+.
You should note that GDI/GDI+ (System.Drawing namespace) is not officially supported in ASP.NET - see "Caution" in http://msdn.microsoft.com/en-us/library/system.drawing.aspx.
WIC is supposed to be used instead GDI+ (see http://weblogs.asp.net/bleroy/archive/2009/12/10/resizing-images-from-the-server-using-wpf-wic-instead-of-gdi.aspx)
Said that, many had successfully use GDI+ in ASP.NET. Most probably, you should attempt saving image into memory stream (or on file) and then writing saved image into the response. See this link for details: http://www.west-wind.com/weblog/posts/2006/Oct/19/Common-Problems-with-rendering-Bitmaps-into-ASPNET-OutputStream
Another work-around can be related to user account. Apparently, GDI/GDI+ is bound to device context (screen, printer etc) and they may not be available under service accounts. So you may try running your ASP.NET code on some normal user account if that helps or not.
You may need to try explicitly encoding the image save.
Have a look at the code example at the bottom of this MSDN documentation on Image.Save
Image.Save Method (String, ImageCodecInfo, EncoderParameters)
The same actions can be applied to your save.
However, it could also possibly be that your objImgManager is disposing of the buffer where the image is stored before you can save it.
Bitmap ImageBitmap = objImgManager.GetBitmapFromBytes(ImageData);
You can get around this by creating a copy of the image by doing this:
Bitmap ImageBitmap = new Bitmap(objImgManager.GetBitmapFromBytes(ImageData));
Related
In an ASP.NET application, is there a way to get the nlog output to go in a buffer in memory?
I'd like to make a circular buffer that would display the log's content on a web page.
What about writing to the memory target?
Then you could read them as follow:
var target = LogManager.Configuration.FindTargetByName<MemoryTarget>("target1");
var logs = target.Logs;
You can specify custom targets with NLog: https://github.com/nlog/NLog/wiki/Targets
It does not look like one exists that does exactly what you need, but you can write your own!
https://github.com/NLog/NLog/wiki/Extending-NLog
Why don't you read tail n-lines from the actual log file and display it? Or send to a database and display the table? Seems that this will added more overhead and memory constants to the ASP.Net site.
I wanted to place a google static map in a pdffile generated by using the fpdf extension.
and used code to make a tempfile first. Using this code. However I run into an error ('can't open image file').
// define the mapurl
$fullURL = 'http://maps.googleapis.com/maps/api/staticmap?center=Amsterdam&z=14&size=100x100&sensor=false';
// create a tempfile
$tempF = tmpfile();
fwrite($tempF, file_get_contents($fullURL));
//Rewind to the start of file
rewind($tempF);
// place the image in the pdf.
if (!empty($tempF)) {
$this->Image($tempF,$start_x, $this->GetY(),0,100);
}
fclose($tempF); // remove the tempfile
Note: OP's own answer extracted from the question.
Then I found out it can be done much easier... I don't need to use a tempfile. If I put the url in the image variable and give a format the thing works in two lines.. I don't use the height and width parameters in fpdf because they stretch the image into a unreadable form.
$fullURL = "http://maps.googleapis.com/maps/api/staticmap?center=". \
$row-Google_coor."&zoom=10&size=2200x200\
&markers=color:blue%7Clabel:D%7C".$row->Google_coor;
$this->Image($fullURL,$start_x, $this->GetY(),0,0,"PNG");
Saddly when using this function sometimes the program returns an error. The image could not be found. But when I put the url string in the browser the image shows up fine. Hopefully this is also a minor fallback.
Lets see if I find a solution on the google apis site https://developers.google.com/maps/documentation/staticmaps/?csw=1
For what it is worth I had this problem as well, I found that my server had an outgoing connections manager, this would automatically block the request, I had to add the ip, once this was done it worked fine.
I had a problem similiar to the OP's. I was getting an error when using FPDF and trying to get the static map image from Google Maps and into my PDF. I was getting "FPDF error: Missing or incorrect image file". What I did is to register in the Google Developer Console and get an API Key, just like they say in the documentation
Hope this helps.
You can download the image and use it with fpdf
$url ="https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614,-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284&key=".$googleApiKey;
$img = __DIR__.'/img/map.png';
file_put_contents($img, file_get_contents($url));
$pdf->Image($img,null,null,170);
Hope that this will be helpful
I used something like
Dim i As String
i = Server.MapPath("~/photos/") + fileName
Only once in a project that was working and online, on the offline version, when I run it on my machine, its working, no errors, I uploaded it, it gave me an error like:
'~/photos/http://www.MyURL.com/photos/4411568359267Pic003.jpg' is not a valid virtual path.
Indicating a line in my code:
var marker = new GMarker(new GLatLng(<%=coordinates%>));
This have never happened before, and I don't know where to start troubleshooting as this script -Google Maps- doesn't even need images, i tried to comment it out, it gave me the same error but on a different script this time, the one that show formatting toolbar for the text areas
Line 8: new nicEditor({buttonList : ['fontSize','fontFamily','fontFormat','bold','italic','underline','strikethrough','forecolor','bgcolor','removeformat'], iconsPath : '../nicEdit/nicEditorIcons.gif'}).panelInstance('<%= txtDescription.ClientID %>');
..please HELP :'(
can you post your fileupload aspx page and your function so we can troubleshoot it.
"'~/photos/http://www.MyURL.com/photos/4411568359267Pic003.jpg'"
look closely at the url, it is tacking the full url on to "i". what type of control or you using, a generic or a server side control
solved, turns out the error is due to old database records when i was storing the whole path in the fileName...Thanks Joshua and Keltex
I have a parts application that is built on Flex 3 and I having a problem with the parts list. The parts list uses two objects:
Part
ArrayOfPart
I cache the parts list locally in a SharedObject. This has been working fine for a couple months. I added a new Boolean property to the Part class and re-imported web services (which shouldn't have affected anything) and now, I can save the ArrayOfPart SharedObject fine, but when I try to retrieve the SharedObject, I get "ArgumentError: Error #1063: Argument count mismatch on ArrayOfPart. Expected 1, got 0. AND then it DELETES my SOL file completely.
(I used RegisterClass on Part and ArrayOfPart, so they both are serializable to SharedObjects)
Here's the steps I followed to get the error:
Save the shared object:
so = SharedObject.getLocal("PartsList");
so.data.AllParts = AllParts;
so.flush();
Verify the SharedObject:
The SharedObject PartsList.sol exists where it should
I opened the SharedObject file and the ArrayOfPart data looks valid
Restart the application and it retrieves the parts list from the SharedObject. This is the line that throws the Error #1063 and causes the sol file to be deleted:
so = SharedObject.getLocal("PartsList");
It looks like the data may not be well-formed when it's saved in the SharedObject? Is this possible?
I solved my own problem.
The ArrayOfPart had a constructor with a parameter. When I removed the constructor, the error went away.
By the way, this was Flash 9.
This looks like a tough one.
I don't think it is a formation or serialization issue. Maybe something else in your code is writing to the shared object?
Is there any way you can share your source? This would help as I personally don't have enough info to diagnose what is going on w/o seeing the rest of the code :(
Also....
Is there anything else that is writing to or changing this shared object?
Are all my objects created in AS3 or are some in MXML (I have noticed that the Flex compiler does not always do a good job figuring out MXML).
What version of Flash do I have?
If its Flash 10 does the same problem occur in Flash 9?
Does this happen in all browsers and on all platforms?
Just a hunch, but, since I installed Flash Player 10, I am seeing lots of errors with Shared Objects when browsing. Could it be related to newly introduced sandbox/security restrictions?
We're having problems with an ASP.NET application which allows users to upload, and crop images. The images are all scaled to fixed sizes afterwards. We basically run out of memory when a large file is processed; it seems that the handling of JPEG is rather inefficient -- we're using System.Drawing.BitMap. Do you have any general advice, and perhaps some pointers to a more efficient image handling library? What experiences do you have?
I had the same problem, the solution was to use System.Drawing.Graphics to do the transformations and dispose every bitmap object as soon as I was finished with it. Here's a sample from my library (resizing) :
public Bitmap ApplyTo(Bitmap bitmap)
{
using (bitmap)
{
Bitmap newBitmap = new Bitmap(bitmap, CalculateNewSize(bitmap));
using (Graphics graphics = Graphics.FromImage(newBitmap))
{
graphics.SmoothingMode =
SmoothingMode.None;
graphics.InterpolationMode =
InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality =
CompositingQuality.HighQuality;
graphics.DrawImage(
bitmap,
new Rectangle(0, 0, newBitmap.Width, newBitmap.Height));
}
return newBitmap;
}
}
I found imageresizer and its great. and good API. Works Great.
Downloaded from Visual studio 2010 Extension Manager: http://nuget.org/.
Easy Steps to download API in VS-2010:
1). Install Extension http://nuget.org/.
3). Find and Install ImageResizing
4).Then Code: (I m using here cropping. you can use any) Documentation on imageresizing.net
string uploadFolder = Server.MapPath(Request.ApplicationPath + "images/");
FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);
//The resizing settings can specify any of 30 commands.. See http://imageresizing.net for details.
ResizeSettings resizeCropSettings = new ResizeSettings("width=200&height=200&format=jpg&crop=auto");
//Generate a filename (GUIDs are safest).
string fileName = Path.Combine(uploadFolder, System.Guid.NewGuid().ToString());
//Let the image builder add the correct extension based on the output file type (which may differ).
fileName = ImageBuilder.Current.Build(uploadFolder + FileUpload1.FileName, fileName, resizeCropSettings, false, true);
Try!!! its very awsumm and easy to use. thanks.
A couple of thoughts spring to mind -
What size of images do you allow
your users to upload and can you
impose restrictions on this?
When you're using the
System.Drawing.Bitmap class, are you
remembering to dispose of it
correctly? We found one of the primary causes of System.OutOfMemoryException exceptions on our shared hosting platform was users not disposing of Bitmap objects correctly.
Kev
There was an older bug with .net that all images would default to 32 bits per pixel - at this size you can exhaust your memory pretty fast. Please use PixelFormat structure to make sure this is not the case for your problem.
This link might help: http://msdn.microsoft.com/en-us/library/aa479306.aspx
Do you perhaps have a stack trace to look at?
I have also done some image editing after a user has uploaded an image. The only problems that I ran into were restrictions on file upload size on the browsers and timeouts. But nothing related to .Net's libraries.
Something else to consider. If you are doing multiple images or have some dangerous looping somewhere then are you making sure to flush() and dispose() things.