नहीं दिखा रही हैं, मैं यहां अपने विट्स पर हूं।HTTP हैंडलर एएसपी.Net का उपयोग कर छवियों का आकार बदलना, कुछ छवियां
मेरे पास एक HTTP हैंडलर (ImageHandler.ashx) है जो छवियों (आकार बदलता है) भेजता है, यह एक मानक HTTP हैंडलर (इसे पुन: प्रयोज्य सत्य और गलत के साथ आज़माया जाता है) जो छवि का उपयोग करता है। थेट थंबनेल इमेज का आकार बदलने और थंबनेल वापस करने के लिए।
मेरे पास एएसपी डातालिस्ट नियंत्रण है जिसमें एक HTML छवि नियंत्रण वाला एक टेबल है।
<asp:DataList ID="listImg" runat="server" RepeatColumns="4" RepeatDirection="Horizontal"
ShowFooter="false" ShowHeader="false">
<ItemTemplate>
<table width="220px">
<tr width="100%">
<td>
<img src="Scripts/ImageHandler.ashx?width=125&image=Upload/<%# DataBinder.Eval(Container.DataItem, "photo") %>"
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
जैसा कि आप पैरामीटर देख सकते हैं हैंडलर की आवश्यकता चौड़ाई और छवि पथ हैं।
यह डातालिस्ट एक डेटाटेबल (छविडेटा) से जुड़ा हुआ है जो प्रदर्शित होने वाली छवियों की सूची प्रदान करता है।
खैर सभी अब तक समझ में आता है, अब यह मुद्दा है - कहें कि मैं 5 छवियां लोड कर रहा हूं, यानी मेरी छविडेटा डेटाटेबल में 5 पंक्तियां हैं, यह एक दिया गया है कि केवल 3-4 छवियां प्रदर्शित की जाएंगी, शेष बस एक लाल एक्स के साथ आओ, जैसे कि जब आपके पास कोई छवि नहीं है। अब यदि आप कोड को देखते हैं और छवि स्रोत पर नेविगेट करते हैं -
http://localhost:3540/Scripts/ImageHandler.ashx?width=150&image=Upload/Test123.jpg
आप छवि देखेंगे, वे सभी वहां कोई गायब छवियां नहीं हैं। पुनः लोड करें और वे वापस आ गए हैं।
मैंने फ़ायरफ़ॉक्स में इसे चलाया और फ़ायरबग खोल दिया और जब मैंने छवि टैब को देखा, तो सभी छवियों को फायरबग के अनुसार वापस कर दिया गया था (स्थिति 200 ठीक है और मैं प्रतिक्रिया टैब में छवि देखता हूं), यह बस वेबसर्वर की तरह है उनमें से कुछ प्रदर्शित नहीं करता है। ध्यान दें कि यह हमेशा ऐसी छवियां नहीं होती है जो लापता होने वाली प्रक्रिया/लोड करने के लिए सबसे लंबे समय तक ले जाती हैं, कुछ यादृच्छिक हैं।
यहां क्या हो सकता है?
धन्यवाद।
EDIT 1 - हैंडलर कोड (मूल) जोड़ना, हमने इस कोड को विरासत में मिला है। मैंने यहां कोड से कैशिंग हटा दी है, लेकिन एक बार जेनरेट किए गए एफवाईआई थंबनेल कैश किए जाते हैं।
public class ImageHandler : IHttpHandler{
public int _width;
public int _height;
public int _percent;
public string imageURL;
public void ProcessRequest(HttpContext context)
{
try
{
Bitmap bitOutput;
string appPath = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ImagePath"]);
String strArquivo = appPath + context.Request.QueryString["image"].Replace("/", "\\");
if (!(String.IsNullOrEmpty(context.Request["width"])))
{
Bitmap bitInput = GetImage(context);
if (SetHeightWidth(context, bitInput))
{ bitOutput = ResizeImage(bitInput, _width, _height, _percent); }
else { bitOutput = bitInput; }
context.Response.ContentType = "image/jpeg";
bitOutput.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
return;
}
catch (Exception ex) { /*HttpContext.Current.Response.Write(ex.Message);*/ }
}
/// <summary>
/// Get the image requested via the query string.
/// </summary>
public Bitmap GetImage(HttpContext context)
{
try
{
if (context.Cache[("ImagePath-" + context.Request.QueryString["image"])] == null)
{
string appPath = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["ImagePath"]);
appPath = appPath + context.Request.QueryString["image"].Replace("/", "\\");
Bitmap bitOutput;
imageURL = appPath;
bitOutput = new Bitmap(appPath);
return bitOutput;
}
else
{
return (Bitmap)context.Cache[("ImagePath-" + context.Request.QueryString["image"])];
}
}
catch (Exception ex) { throw ex; }
}
/// <summary>
/// Set the height and width of the handler class.
/// </summary>
public bool SetHeightWidth(HttpContext context, Bitmap bitInput)
{
try
{
double inputRatio = Convert.ToDouble(bitInput.Width)/Convert.ToDouble(bitInput.Height);
if (!(String.IsNullOrEmpty(context.Request["width"])) && !(String.IsNullOrEmpty(context.Request["height"])))
{
_width = Int32.Parse(context.Request["width"]);
_height = Int32.Parse(context.Request["height"]);
return true;
}
else if (!(String.IsNullOrEmpty(context.Request["width"])))
{
_width = Int32.Parse(context.Request["width"]);
_height = Convert.ToInt32((_width/inputRatio));
if (_width == 400 &&_height > 500)
{
_height = 500;
_width = Convert.ToInt32(500 * inputRatio);
}
else if (_width == 125 && _height > 200)
{
_height = 200;
_width = Convert.ToInt32(200 * inputRatio);
}
return true;
}
else if (!(String.IsNullOrEmpty(context.Request["height"])))
{
_height = Int32.Parse(context.Request["height"]);
_width = Convert.ToInt32((_height * inputRatio));
return true;
}
else if (!(String.IsNullOrEmpty(context.Request["percent"])))
{
_height = bitInput.Height;
_width = bitInput.Width;
_percent = Int32.Parse(context.Request["percent"]);
return true;
}
else
{
_height = bitInput.Height;
_width = bitInput.Width;
return false;
}
}
catch (Exception ex) { throw ex; }
}
/// <summary>
/// Resizes bitmap using high quality algorithms.
/// </summary>
public static Bitmap ResizeImage(Bitmap originalBitmap, int newWidth, int newHeight, int newPercent)
{
try
{
if (newPercent != 0)
{
newWidth = Convert.ToInt32(originalBitmap.Width * (newPercent * .01));
newHeight = Convert.ToInt32(originalBitmap.Height * (newPercent * .01));
}
Bitmap inputBitmap = originalBitmap;
Bitmap resizedBitmap = new Bitmap(newWidth, newHeight, PixelFormat.Format64bppPArgb);
Graphics g = Graphics.FromImage(resizedBitmap);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Rectangle rectangle = new Rectangle(0, 0, newWidth, newHeight);
g.DrawImage(inputBitmap, rectangle, 0, 0, inputBitmap.Width, inputBitmap.Height, GraphicsUnit.Pixel);
g.Dispose();
return resizedBitmap;
}
catch (Exception ex) { throw ex; }
}
public bool IsReusable
{
get
{
return true;
}
}}
संपादित 2 कोई इस पूरी बात का परीक्षण करने के लिए, यहाँ कोड है कि DataTable (ImageData) बनाने के लिए एक पूर्वनिर्धारित फ़ोल्डर से यादृच्छिक पर छवियों को चुनता है कि aspDataList (listImg) बाध्य है चाहता है करने के लिए - कोशिश
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Server.MapPath("Upload"));
System.IO.FileInfo[] files = dirInfo.GetFiles();
int fileCount = files.Length;
System.Data.DataTable ImageData = new System.Data.DataTable();
System.Data.DataColumn dCol = new System.Data.DataColumn("photo");
ImageData.Columns.Add(dCol);
System.Random rnd = new Random();
int nxtNumber = 0;
System.Data.DataRow dRow = null;
string fileName = string.Empty;
for (int i = 0; i < 20; i++)
{
dRow = ImageData.NewRow();
nxtNumber = rnd.Next(fileCount);
while (!files[nxtNumber].Extension.Equals(".jpg"))
{
nxtNumber = rnd.Next(fileCount);
}
fileName = files[nxtNumber].Name;
dRow["photo"] = fileName;
ImageData.Rows.Add(dRow);
}
listImg.DataSource = ImageData;
listImg.DataBind();
क्या आप हैंडलर कोड पोस्ट कर सकते हैं? हो सकता है कि वहां कुछ ऐसा हो जो कभी-कभी खाली छवि लौट रहा हो। साथ ही, जब आप कहते हैं कि छवियों को फायरबग में 200 स्थितियां मिलीं, तो क्या उनके पास सामग्री भी थी? यह आपको बाइट्स की संख्या को वापस बताना चाहिए। –
हां यह सामग्री (छवि) और आकार दिखाता है। साथ ही यह हमेशा एक ही छवि नहीं है जो खाली दिखता है, अगर मैं एक ही सेट को कई कैशिंग के साथ प्रदर्शित करता हूं, तो यह यादृच्छिक छवि गायब हो जाती है। एक बार जब मैं वहां से अनावश्यक सामान हटा देता हूं, तो मैं हैंडलर कोड को थोड़ा सा पोस्ट करूंगा। – Rakesh
यह एक उत्तर नहीं है, लेकिन एक सुझाव है। आप इस nuget पैकेज को देखना चाह सकते हैं: http://nuget.org/packages/ImageResizer इसमें gif resize आदि के लिए कई अतिरिक्त पैकेज हैं। –