1 Star 3 Fork 0

CIMYong/ImageViewer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
KP-DrawObject.cs 45.46 KB
一键复制 编辑 原始数据 按行查看 历史
CIMYong 提交于 2020-03-11 10:03 . 初始化
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace KaiwaProjects
{
public class KP_DrawObject
{
private KpImageViewer KpViewer;
private Rectangle boundingRect;
private Point dragPoint;
private bool dragging;
private Bitmap bmp;
private Bitmap bmpPreview;
private MultiPageImage multiBmp;
private GifImage gifBmp;
private double zoom = 1.0;
static private int panelWidth = 0;
static private int panelHeight = 0;
static private int previewPanelWidth = 0;
static private int previewPanelHeight = 0;
private int rotation = 0;
private bool multiFrame = false;
private bool multiPage = false;
private int pages = 1;
private int currentPage = 0;
public Rectangle BoundingBox
{
get { return boundingRect; }
}
public void Dispose()
{
if (this.Image != null)
{
this.Image.Dispose();
}
}
public bool IsDragging
{
get { return dragging; }
}
public GifImage Gif
{
get { return gifBmp; }
}
public Size OriginalSize
{
get
{
if (this.Image != null)
{
if (multiFrame == true)
{
if (this.gifBmp != null)
{
if (this.gifBmp.Rotation == 0 || this.gifBmp.Rotation == 180)
{
return this.gifBmp.CurrentFrame.Size;
}
else
{
return new Size(this.gifBmp.CurrentFrame.Height, this.gifBmp.CurrentFrame.Width);
}
}
return Size.Empty;
}
else
{
return this.Image.Size;
}
}
else
{
return Size.Empty;
}
}
}
public Size CurrentSize
{
get { if (boundingRect != null) { return new Size(boundingRect.Width, boundingRect.Height); } else { return Size.Empty; } }
}
public bool MultiPage
{
get { return multiPage; }
}
public int Pages
{
get { return pages; }
}
public int CurrentPage
{
get { return currentPage; }
}
public double Zoom
{
get { return zoom; }
}
public int Rotation
{
get { return rotation; }
set
{
// Making sure that the rotation is only 0, 90, 180 or 270 degrees!
if (value == 90 || value == 180 || value == 270 || value == 0)
{
rotation = value;
}
}
}
public Bitmap GetPage(int pageNumber)
{
if (this.multiBmp == null)
{
return null;
}
int pages = this.multiBmp.Image.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
if (pages > pageNumber && pageNumber >= 0)
{
this.multiBmp.Image.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, pageNumber);
return new Bitmap(this.multiBmp.Image);
}
return null;
}
public int ImageWidth
{
get
{
if (multiFrame == true)
{
if (gifBmp != null)
{
if (gifBmp.Rotation == 0 || gifBmp.Rotation == 180)
{
return gifBmp.CurrentFrame.Width;
}
else
{
return gifBmp.CurrentFrame.Height;
}
}
return 0;
}
else
{
return this.Image.Width;
}
}
}
public int ImageHeight
{
get
{
if (multiFrame == true)
{
if (gifBmp != null)
{
if (gifBmp.Rotation == 0 || gifBmp.Rotation == 180)
{
return gifBmp.CurrentFrame.Height;
}
else
{
return gifBmp.CurrentFrame.Width;
}
}
return 0;
}
else
{
return this.Image.Height;
}
}
}
public Bitmap Image
{
get
{
if (this.multiFrame == true)
{
return (Bitmap)gifBmp.CurrentFrame;
}
else if (this.multiPage == true)
{
if (multiBmp != null)
{
return multiBmp.Page;
}
else
{
return null;
}
}
else
{
return bmp;
}
}
set
{
try
{
if (value != null)
{
currentPage = 0;
// No memory leaks here!
if (this.bmp != null)
{
this.bmp.Dispose();
this.bmp = null;
}
if (this.multiBmp != null)
{
this.multiBmp.Dispose();
this.multiBmp = null;
}
try
{
FrameDimension gifDimension = new FrameDimension(value.FrameDimensionsList[0]);
int gifFrames = value.GetFrameCount(gifDimension);
if (gifFrames > 1)
{
multiFrame = true;
}
else
{
multiFrame = false;
}
if (!multiFrame)
{
//Gets the total number of frames in the .tiff file
pages = value.GetFrameCount(FrameDimension.Page);
if (pages > 1) { multiPage = true; } else { multiPage = false; }
}
}
catch
{
multiPage = false;
pages = 1;
}
if (multiFrame == true)
{
this.gifBmp = new GifImage(this.KpViewer, value);
}
else if (multiPage == true)
{
this.bmp = null;
this.multiBmp = new MultiPageImage(value);
}
else
{
this.bmp = value;
this.multiBmp = null;
}
// Initial rotation adjustments
if (rotation != 0)
{
if (rotation == 180)
{
this.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
boundingRect = new Rectangle(0, 0, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
}
else
{
if (rotation == 90)
{
this.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
else if (rotation == 270)
{
this.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
// Flip the X and Y values
boundingRect = new Rectangle(0, 0, (int)(this.ImageHeight * zoom), (int)(this.ImageWidth * zoom));
}
}
else
{
this.Image.RotateFlip(RotateFlipType.RotateNoneFlipNone);
boundingRect = new Rectangle(0, 0, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
}
zoom = 1.0;
bmpPreview = CreatePreviewImage();
FitToScreen();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
}
public Image PreviewImage
{
get { return bmpPreview; }
}
public string ImagePath
{
set
{
try
{
// No memory leaks here!
if (this.bmp != null)
{
this.bmp.Dispose();
this.bmp = null;
}
if (this.multiBmp != null)
{
this.multiBmp.Dispose();
this.multiBmp = null;
}
Bitmap temp = null;
// Make sure it does not crash on incorrect image formats
try
{
//temp = (Bitmap)Bitmap.FromFile(value);
temp = new Bitmap(value);
}
catch
{
temp = null;
System.Windows.Forms.MessageBox.Show("ImageViewer error: Incorrect image format!");
}
if (temp != null)
{
currentPage = 0;
try
{
string extension = Path.GetExtension(value);
if (extension == ".gif")
{
FrameDimension gifDimension = new FrameDimension(temp.FrameDimensionsList[0]);
int gifFrames = temp.GetFrameCount(gifDimension);
if (gifFrames > 1)
{
multiFrame = true;
}
else
{
multiFrame = false;
}
}
else
{
multiFrame = false;
//Gets the total number of frames in the .tiff file
pages = temp.GetFrameCount(FrameDimension.Page);
if (pages > 1) { multiPage = true; } else { multiPage = false; }
}
}
catch
{
multiPage = false;
pages = 1;
}
if (multiFrame == true)
{
this.gifBmp = new GifImage(this.KpViewer, temp);
}
else if (multiPage == true)
{
this.bmp = null;
this.multiBmp = new MultiPageImage(temp);
}
else
{
this.bmp = temp;
this.multiBmp = null;
}
// Initial rotation
if (rotation != 0)
{
if (rotation == 180)
{
this.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
boundingRect = new Rectangle(0, 0, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
}
else
{
if (rotation == 90)
{
this.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
else if (rotation == 270)
{
this.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
// Flipping X and Y values!
boundingRect = new Rectangle(0, 0, (int)(this.ImageHeight * zoom), (int)(this.ImageWidth * zoom));
}
}
else
{
this.Image.RotateFlip(RotateFlipType.RotateNoneFlipNone);
boundingRect = new Rectangle(0, 0, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
}
zoom = 1.0;
bmpPreview = CreatePreviewImage();
FitToScreen();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
}
public KP_DrawObject(KpImageViewer KpViewer, Bitmap bmp)
{
try
{
this.KpViewer = KpViewer;
// Initial dragging to false and an Image.
dragging = false;
this.Image = bmp;
this.Image.RotateFlip(RotateFlipType.RotateNoneFlipNone);
boundingRect = new Rectangle(0, 0, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
private System.Drawing.Imaging.ImageCodecInfo GetCodec(string type)
{
System.Drawing.Imaging.ImageCodecInfo[] info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < info.Length; i++)
{
string EnumName = type.ToString();
if (info[i].FormatDescription.Equals(EnumName))
{
return info[i];
}
}
return null;
}
public void SetPage(int page)
{
int p = page - 1;
try
{
if (this.Image != null && this.multiPage == true)
{
if (p < this.pages && p >= 0)
{
currentPage = p;
this.multiBmp.SetPage(p);
this.multiBmp.Rotate(this.rotation);
// No memory leaks here!
if (this.bmpPreview != null)
{
this.bmpPreview.Dispose();
this.bmpPreview = null;
}
this.bmpPreview = CreatePreviewImage();
AvoidOutOfScreen();
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void NextPage()
{
try
{
if (this.Image != null && this.multiPage == true)
{
int nextPage = this.currentPage + 1;
if (nextPage < this.pages)
{
currentPage = nextPage;
this.multiBmp.SetPage(currentPage);
this.multiBmp.Rotate(this.rotation);
// No memory leaks here!
if (this.bmpPreview != null)
{
this.bmpPreview.Dispose();
this.bmpPreview = null;
}
this.bmpPreview = CreatePreviewImage();
AvoidOutOfScreen();
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void PreviousPage()
{
try
{
if (this.Image != null && this.multiPage == true)
{
int prevPage = this.currentPage - 1;
if (prevPage >= 0)
{
currentPage = prevPage;
this.multiBmp.SetPage(currentPage);
this.multiBmp.Rotate(this.rotation);
// No memory leaks here!
if (this.bmpPreview != null)
{
this.bmpPreview.Dispose();
this.bmpPreview = null;
}
this.bmpPreview = CreatePreviewImage();
AvoidOutOfScreen();
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public KP_DrawObject(KpImageViewer KpViewer)
{
try
{
this.KpViewer = KpViewer;
// Initial dragging to false and No image.
dragging = false;
this.bmp = null;
this.multiBmp = null;
this.gifBmp = null;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
static public void UpdatePanelsize(int w, int h)
{
try
{
// Making sure panel size stays the same
panelWidth = w;
panelHeight = h;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
static public void UpdatePreviewPanelsize(int w, int h)
{
try
{
// Making sure preview panel size stays the same
previewPanelWidth = w;
previewPanelHeight = h;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void Rotate90()
{
try
{
if (this.Image != null)
{
int tempWidth = boundingRect.Width;
int tempHeight = boundingRect.Height;
boundingRect.Width = tempHeight;
boundingRect.Height = tempWidth;
rotation = (rotation + 90) % 360;
if (this.multiFrame == true)
{
this.gifBmp.Rotate(90);
}
else if (this.MultiPage == true)
{
if (this.multiBmp != null)
{
this.multiBmp.Rotate(90);
}
}
else
{
this.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);
}
AvoidOutOfScreen();
// No memory leaks here!
if (this.bmpPreview != null)
{
this.bmpPreview.Dispose();
this.bmpPreview = null;
}
this.bmpPreview = CreatePreviewImage();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void Rotate180()
{
try
{
if (this.Image != null)
{
int tempWidth = boundingRect.Width;
int tempHeight = boundingRect.Height;
boundingRect.Width = tempHeight;
boundingRect.Height = tempWidth;
rotation = (rotation + 180) % 360;
if (this.multiFrame == true)
{
this.gifBmp.Rotate(180);
}
else if (this.MultiPage == true)
{
if (this.multiBmp != null)
{
this.multiBmp.Rotate(180);
}
}
else
{
this.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
}
AvoidOutOfScreen();
// No memory leaks here!
if (this.bmpPreview != null)
{
this.bmpPreview.Dispose();
this.bmpPreview = null;
}
this.bmpPreview = CreatePreviewImage();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void Rotate270()
{
try
{
if (this.Image != null)
{
int tempWidth = boundingRect.Width;
int tempHeight = boundingRect.Height;
boundingRect.Width = tempHeight;
boundingRect.Height = tempWidth;
rotation = (rotation + 270) % 360;
if (this.multiFrame == true)
{
this.gifBmp.Rotate(270);
}
else if (this.MultiPage == true)
{
if (this.multiBmp != null)
{
this.multiBmp.Rotate(270);
}
}
else
{
this.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);
}
AvoidOutOfScreen();
// No memory leaks here!
if (this.bmpPreview != null)
{
this.bmpPreview.Dispose();
this.bmpPreview = null;
}
this.bmpPreview = CreatePreviewImage();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
private Bitmap RotateCenter(Bitmap bmpSrc, float theta)
{
Matrix mRotate = new Matrix();
mRotate.Translate(bmpSrc.Width / -2, bmpSrc.Height / -2, MatrixOrder.Append);
mRotate.RotateAt(theta, new Point(0, 0), MatrixOrder.Append);
using (GraphicsPath gp = new GraphicsPath())
{ // transform image points by rotation matrix
gp.AddPolygon(new Point[] { new Point(0, 0), new Point(bmpSrc.Width, 0), new Point(0, bmpSrc.Height) });
gp.Transform(mRotate);
PointF[] pts = gp.PathPoints;
// create destination bitmap sized to contain rotated source image
Rectangle bbox = RotateBoundingBox(bmpSrc, mRotate);
Bitmap bmpDest = new Bitmap(bbox.Width, bbox.Height);
using (Graphics gDest = Graphics.FromImage(bmpDest))
{ // draw source into dest
Matrix mDest = new Matrix();
mDest.Translate(bmpDest.Width / 2, bmpDest.Height / 2, MatrixOrder.Append);
gDest.Transform = mDest;
gDest.DrawImage(bmpSrc, pts);
gDest.DrawRectangle(Pens.Red, bbox);
//drawAxes(gDest, Color.Red, 0, 0, 1, 100, "");
return bmpDest;
}
}
}
private static Rectangle RotateBoundingBox(Image img, System.Drawing.Drawing2D.Matrix matrix)
{
GraphicsUnit gu = new GraphicsUnit();
Rectangle rImg = Rectangle.Round(img.GetBounds(ref gu));
// Transform the four points of the image, to get the resized bounding box.
Point topLeft = new Point(rImg.Left, rImg.Top);
Point topRight = new Point(rImg.Right, rImg.Top);
Point bottomRight = new Point(rImg.Right, rImg.Bottom);
Point bottomLeft = new Point(rImg.Left, rImg.Bottom);
Point[] points = new Point[] { topLeft, topRight, bottomRight, bottomLeft };
GraphicsPath gp = new GraphicsPath(points, new byte[] { (byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line });
gp.Transform(matrix);
return Rectangle.Round(gp.GetBounds());
}
private Bitmap CreatePreviewImage()
{
// 148 && 117 as initial and default size for the preview panel.
Rectangle previewRect = new Rectangle(0, 0, 148, 117);
double x_ratio = (double)previewRect.Width / (double)this.BoundingBox.Width;
double y_ratio = (double)previewRect.Height / (double)this.BoundingBox.Height;
if ((this.BoundingBox.Width <= previewRect.Width) && (this.BoundingBox.Height <= previewRect.Height))
{
previewRect.Width = this.BoundingBox.Width;
previewRect.Height = this.BoundingBox.Height;
}
else if ((x_ratio * this.BoundingBox.Height) < previewRect.Height)
{
previewRect.Height = Convert.ToInt32(Math.Ceiling(x_ratio * this.BoundingBox.Height));
previewRect.Width = previewRect.Width;
}
else
{
previewRect.Width = Convert.ToInt32(Math.Ceiling(y_ratio * this.BoundingBox.Width));
previewRect.Height = previewRect.Height;
}
Bitmap bmp = new Bitmap(previewRect.Width, previewRect.Height);
if (multiFrame == true)
{
if (this.gifBmp != null)
{
using (Graphics g = Graphics.FromImage(bmp))
{
if (this.gifBmp.Rotation != 0)
{
g.DrawImage(RotateCenter(this.gifBmp.CurrentFrame, this.gifBmp.Rotation), previewRect);
}
else
{
g.DrawImage(this.gifBmp.CurrentFrame, previewRect);
}
}
}
}
else
{
using (Graphics g = Graphics.FromImage(bmp))
{
if (this.Image != null)
{
g.DrawImage(this.Image, previewRect);
}
}
}
return bmp;
}
public void ZoomToSelection(Rectangle selection, Point ptPbFull)
{
int x = (selection.X - ptPbFull.X);
int y = (selection.Y - ptPbFull.Y);
int width = selection.Width;
int height = selection.Height;
// So, where did my selection start on the entire picture?
int selectedX = (int)((double)(((double)boundingRect.X - ((double)boundingRect.X * 2)) + (double)x) / zoom);
int selectedY = (int)((double)(((double)boundingRect.Y - ((double)boundingRect.Y * 2)) + (double)y) / zoom);
int selectedWidth = width;
int selectedHeight = height;
// The selection width on the scale of the Original size!
if (zoom < 1.0 || zoom > 1.0)
{
selectedWidth = Convert.ToInt32((double)width / zoom);
selectedHeight = Convert.ToInt32((double)height / zoom);
}
// What is the highest possible zoomrate?
double zoomX = ((double)panelWidth / (double)selectedWidth);
double zoomY = ((double)panelHeight / (double)selectedHeight);
double newZoom = Math.Min(zoomX, zoomY);
// Avoid Int32 crashes!
if (newZoom * 100 < Int32.MaxValue && newZoom * 100 > Int32.MinValue)
{
SetZoom(newZoom);
selectedWidth = (int)((double)selectedWidth * newZoom);
selectedHeight = (int)((double)selectedHeight * newZoom);
// Center the selected area
int offsetX = 0;
int offsetY = 0;
if (selectedWidth < panelWidth)
{
offsetX = (panelWidth - selectedWidth) / 2;
}
if (selectedHeight < panelHeight)
{
offsetY = (panelHeight - selectedHeight) / 2;
}
boundingRect.X = (int)((int)((double)selectedX * newZoom) - ((int)((double)selectedX * newZoom) * 2)) + offsetX;
boundingRect.Y = (int)((int)((double)selectedY * newZoom) - ((int)((double)selectedY * newZoom) * 2)) + offsetY;
AvoidOutOfScreen();
}
}
public void JumpToOrigin(int x, int y, int width, int height, int pWidth, int pHeight)
{
try
{
double zoom = (double)boundingRect.Width / (double)width;
int originX = (int)(x * zoom);
int originY = (int)(y * zoom);
originX = originX - (originX * 2);
originY = originY - (originY * 2);
boundingRect.X = originX + (pWidth / 2);
boundingRect.Y = originY + (pHeight / 2);
AvoidOutOfScreen();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void JumpToOrigin(int x, int y, int width, int height)
{
try
{
boundingRect.X = (x - (width / 2)) - ((x - (width / 2)) * 2);
boundingRect.Y = (y - (height / 2)) - ((y - (height / 2)) * 2);
AvoidOutOfScreen();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public Point PointToOrigin(int x, int y, int width, int height)
{
try
{
double zoomX = (double)width / (double)boundingRect.Width;
double zoomY = (double)height / (double)boundingRect.Height;
if (width > panelWidth)
{
int oldX = (boundingRect.X - (boundingRect.X * 2)) + (panelWidth / 2);
int oldY = (boundingRect.Y - (boundingRect.Y * 2)) + (panelHeight / 2);
int newX = (int)(oldX * zoomX);
int newY = (int)(oldY * zoomY);
int originX = newX - (panelWidth / 2) - ((newX - (panelWidth / 2)) * 2);
int originY = newY - (panelHeight / 2) - ((newY - (panelHeight / 2)) * 2);
return new Point(originX, originY);
}
else
{
if (height > panelHeight)
{
int oldY = (boundingRect.Y - (boundingRect.Y * 2)) + (panelHeight / 2);
int newY = (int)(oldY * zoomY);
int originY = newY - (panelHeight / 2) - ((newY - (panelHeight / 2)) * 2);
return new Point(0, originY);
}
else
{
return new Point(0, 0);
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
return new Point(0, 0);
}
}
public void ZoomIn()
{
try
{
if (this.Image != null)
{
// Make sure zoom steps are with 25%
double index = 0.25 - (zoom % 0.25);
if(index != 0)
{
zoom += index;
}
else
{
zoom += 0.25;
}
Point p = PointToOrigin(boundingRect.X, boundingRect.Y, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
boundingRect = new Rectangle(p.X, p.Y, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
AvoidOutOfScreen();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void ZoomOut()
{
try
{
if (this.Image != null)
{
// Make sure zoom steps are with 25% and higher than 0%
if (zoom - 0.25 > 0)
{
if (((zoom - 0.25) % 0.25) != 0)
{
zoom -= zoom % 0.25;
}
else
{
zoom -= 0.25;
}
}
Point p = PointToOrigin(boundingRect.X, boundingRect.Y, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
boundingRect = new Rectangle(p.X, p.Y, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
AvoidOutOfScreen();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void SetZoom(double z)
{
try
{
if (this.Image != null)
{
zoom = z;
Point p = PointToOrigin(boundingRect.X, boundingRect.Y, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
boundingRect = new Rectangle(p.X, p.Y, (int)(this.ImageWidth * zoom), (int)(this.ImageHeight * zoom));
AvoidOutOfScreen();
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void Scroll(object sender, System.Windows.Forms.MouseEventArgs e)
{
try
{
if (this.Image != null)
{
if (e.Delta < 0)
{
ZoomOut();
}
else
{
ZoomIn();
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void FitToScreen()
{
try
{
if (this.Image != null)
{
double x_ratio = (double)panelWidth / (double)this.ImageWidth;
double y_ratio = (double)panelHeight / (double)this.ImageHeight;
if ((this.ImageWidth <= panelWidth) && (this.ImageHeight <= panelHeight))
{
boundingRect.Width = this.ImageWidth;
boundingRect.Height = this.ImageHeight;
}
else if ((x_ratio * this.ImageHeight) < panelHeight)
{
boundingRect.Height = Convert.ToInt32(Math.Ceiling(x_ratio * this.ImageHeight));
boundingRect.Width = panelWidth;
}
else
{
boundingRect.Width = Convert.ToInt32(Math.Ceiling(y_ratio * this.ImageWidth));
boundingRect.Height = panelHeight;
}
boundingRect.X = 0;
boundingRect.Y = 0;
zoom = ((double)boundingRect.Width / (double)this.ImageWidth);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void AvoidOutOfScreen()
{
try
{
// Am I lined out to the left?
if (boundingRect.X >= 0)
{
boundingRect.X = 0;
}
else if ((boundingRect.X <= (boundingRect.Width - panelWidth) - ((boundingRect.Width - panelWidth) * 2)))
{
if ((boundingRect.Width - panelWidth) - ((boundingRect.Width - panelWidth) * 2) <= 0)
{
// I am too far to the left!
boundingRect.X = (boundingRect.Width - panelWidth) - ((boundingRect.Width - panelWidth) * 2);
}
else
{
// I am too far to the right!
boundingRect.X = 0;
}
}
// Am I lined out to the top?
if (boundingRect.Y >= 0)
{
boundingRect.Y = 0;
}
else if ((boundingRect.Y <= (boundingRect.Height - panelHeight) - ((boundingRect.Height - panelHeight) * 2)))
{
if ((boundingRect.Height - panelHeight) - ((boundingRect.Height - panelHeight) * 2) <= 0)
{
// I am too far to the top!
boundingRect.Y = (boundingRect.Height - panelHeight) - ((boundingRect.Height - panelHeight) * 2);
}
else
{
// I am too far to the bottom!
boundingRect.Y = 0;
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void Drag(Point pt)
{
try
{
if (this.Image != null)
{
if (dragging == true)
{
// Am I dragging it outside of the panel?
if ((pt.X - dragPoint.X > (boundingRect.Width - panelWidth) - ((boundingRect.Width - panelWidth) * 2)) && (pt.X - dragPoint.X < 0))
{
// No, everything is just fine
boundingRect.X = pt.X - dragPoint.X;
}
else if ((pt.X - dragPoint.X > 0))
{
// Now don't drag it out of the panel please
boundingRect.X = 0;
}
else if((pt.X - dragPoint.X < (boundingRect.Width - panelWidth) - ((boundingRect.Width - panelWidth) * 2)))
{
// I am dragging it out of my panel. How many pixels do I have left?
if ((boundingRect.Width - panelWidth) - ((boundingRect.Width - panelWidth) * 2) <= 0)
{
// Make it fit perfectly
boundingRect.X = (boundingRect.Width - panelWidth) - ((boundingRect.Width - panelWidth) * 2);
}
}
// Am I dragging it outside of the panel?
if (pt.Y - dragPoint.Y > (boundingRect.Height - panelHeight) - ((boundingRect.Height - panelHeight) * 2) && (pt.Y - dragPoint.Y < 0))
{
// No, everything is just fine
boundingRect.Y = pt.Y - dragPoint.Y;
}
else if ((pt.Y - dragPoint.Y > 0))
{
// Now don't drag it out of the panel please
boundingRect.Y = 0;
}
else if (pt.Y - dragPoint.Y < (boundingRect.Height - panelHeight) - ((boundingRect.Height - panelHeight) * 2))
{
// I am dragging it out of my panel. How many pixels do I have left?
if ((boundingRect.Height - panelHeight) - ((boundingRect.Height - panelHeight) * 2) <= 0)
{
// Make it fit perfectly
boundingRect.Y = (boundingRect.Height - panelHeight) - ((boundingRect.Height - panelHeight) * 2);
}
}
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void BeginDrag(Point pt)
{
try
{
if (this.Image != null)
{
// Initial drag position
dragPoint.X = pt.X - boundingRect.X;
dragPoint.Y = pt.Y - boundingRect.Y;
dragging = true;
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void EndDrag()
{
try
{
if (this.Image != null)
{
dragging = false;
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
public void Draw(Graphics g)
{
try
{
if (multiFrame == true)
{
if (this.gifBmp.CurrentFrame != null)
{
if (this.gifBmp.Rotation != 0)
{
g.DrawImage(RotateCenter(this.gifBmp.CurrentFrame, this.gifBmp.Rotation), boundingRect);
}
else
{
g.DrawImage(this.gifBmp.CurrentFrame, boundingRect);
}
}
}
if (multiPage == true)
{
if (this.multiBmp != null)
{
if (this.multiBmp.Image != null)
{
g.DrawImage(this.multiBmp.Image, boundingRect);
}
}
}
else
{
if (this.bmp != null)
{
g.DrawImage(this.bmp, boundingRect);
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("ImageViewer error: " + ex.ToString());
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/yongxin1/ImageViewer.git
[email protected]:yongxin1/ImageViewer.git
yongxin1
ImageViewer
ImageViewer
master

搜索帮助