
how to fix "Not enough memory resources are available to complete this operation" asp.net c# ??

Not enough memory resources are available to complete this operation" asp.net c#
I am using loop for my images so I can get multiple images at a time and extract the written data from those images, I am using MODI for my OCR in Visual studio to extract the written data but my problem is ...
after one loop "Not enough memory resources are available to complete this operation" this error appears and only one image is saved in my database, I have tried taking, string builder, string list, and many other helps from internet but still not able to solve my problem. I am stuck at this code for past week
Please help!! if anyone can..
I want to download images in loop to my database and at the same time do Optical Character Recognition.
-Regards from a new learner. thank you
this is my aspx.
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="header pull-left" style="width: 1648px">
<p style="background-color:#0d9dbc; height: 9px; width: 1530px; margin-bottom: 9px;">
<img src="images/logogegor.png" style="height: 63px; width: 118px"/>
</p>
<asp:Panel ID="Panel1" CssClass="Panell" runat="server" Width="1350px" Height="31px">
<label style="width: 1718px; margin-bottom:30px; margin-top:30px; margin-left:30px"><b> OCR </b></label>
</asp:Panel>
<hr style="height:9px; background-color:#0d9dbc; box-shadow:0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19); width :1530px; margin-left: 0px;" />
<div runat="server" id="COUNT">
<asp:FileUpload ID="FileUpload1" runat="server" AllowMultiple="true" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<br/>
<asp:Label ID="lblText" runat="server" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="PictureFileName" HeaderText="File Name" />
<asp:BoundField DataField="Picturetype" Visible="false" HeaderText="File type" />
<asp:BoundField DataField="LoadedData" Visible="false" HeaderText="Loaded Data" />
<asp:BoundField DataField="dataline" HeaderText="Data" />
</Columns>
</asp:GridView>
</div>
</asp:Content>
and this is my aspx.cs
using MODI;
using System;
using System.IO;
using System.Web;
using System.Linq;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Data.Sql;
using System.Threading;
using System.Web.Services;
using System.Drawing.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
public partial class newimagereader : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString);
DataSet DsStockist = new DataSet();
DataTable dtDsStockist = new DataTable();
DataSet DsStockistt = new DataSet();
DataTable dtDsStockistt = new DataTable();
int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM MyPictures";
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
}
protected void Upload(object sender, EventArgs e)
{
foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
{
string filename = Path.GetFileName(postedFile.FileName);
string filePath = System.IO.Path.Combine(Server.MapPath("~/Uploads/" + filename));
FileUpload1.SaveAs(filePath);
string contentType = postedFile.ContentType;
string extractText = ExtractTextFromImage(filePath);
lblText.Text = extractText.Replace(Environment.NewLine, "|");
string crop = string.Empty;
crop = lblText.Text;
if (crop.Length >= 255)
{
crop = crop.Substring(0, 255).ToString();
lblText.Text = crop.ToString();
}
using (Stream fs = postedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = " insert into MyPictures (PictureFileName, Picturetype, LoadedData, dataline) values (@PictureFileName, @Picturetype, @LoadedData, @dataline);";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@PictureFileName", filename);
cmd.Parameters.AddWithValue("@Picturetype", contentType);
cmd.Parameters.AddWithValue("@LoadedData", bytes);
cmd.Parameters.AddWithValue("@dataline", lblText.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
private MODI.Document modiDocument;
private MODI.Images _images;
private MODI.Image modiImage;
private MODI.Layout _layout;
private ManualResetEvent _completedOCR = new ManualResetEvent(false);
private string ExtractTextFromImage(string filePath)
{
Document modiDocument = new Document();
modiDocument.OnOCRProgress += new MODI._IDocumentEvents_OnOCRProgressEventHandler(modiDocument_OnOCRProgress);
_completedOCR.WaitOne(50);
modiDocument.Create(filePath);
modiDocument.OCR(MiLANGUAGES.miLANG_ENGLISH, true, true);
MODI.Image modiImage = (modiDocument.Images[0] as MODI.Image);
_images = modiDocument.Images;
modiImage = (MODI.Image)_images[0];
_layout = modiImage.Layout;
//modiDocument.Save();
string text = _layout.Text;
string extractedText = modiImage.Layout.Text;
modiImage = null;
modiDocument = null;
Disposee();
return extractedText;
}
public object lineToString
{
get;
set;
}
void modiDocument_OnOCRProgress(int Progress, ref bool Cancel)
{
if (Progress == 10)
{
_completedOCR.Set();
}
}
private static void SetComObjectToNull(params object[] objects)
{
for (int i = 0; i < objects.Length; i++)
{
object o = objects[i];
if (o != null)
{
Marshal.FinalReleaseComObject(o);
o = null;
}
}
}
public void Disposee()
{
SetComObjectToNull(_layout, modiImage, _images, modiDocument);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
Microsoft 365 and Office | SharePoint | For business | Windows
